This might seem a bit strange, but I'm having trouble figuring it out. I have no errors, I'm just trying to figure out how to add in a simple points system, which at the moment all I want it to do is keep track of itself. I made an external class for 'coins', and whenever you mouse over one, it disappears. I want to have it so that every time you do this, it adds 1 to the points variable. The problem is that the way I have it set up, it continuously resets every time the mouse over event listener is called, and stays at one, because I've added the event listener to the entire class. I know I could do it from the main class, but I have the 'coins' generated in a for loop so their placement will be random, and I really don't want to have to manually add every single one. Any suggestions on how to fix it so that it goes up one every time instead of resetting to 0?
Here is my current code, I'm only including the bits that are important.
this is the coin class (currently named enemy, since that's what it was originally going to be)
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class enemy extends MovieClip {
var points:uint = 0;
public function enemy() {
// constructor code
this.addEventListener(Event.ENTER_FRAME, moveEnemys);
this.addEventListener(MouseEvent.MOUSE_OVER, disappear);
}
function disappear(event:MouseEvent)
{
this.visible = false;
points ++;
trace(points);
}
function moveEnemys(event:Event)
{
this.x -= 6;
}
}
}
Here is where I add it to the stage in the main class:
var en:enemy;
function addEnemy()
{
for (var i:int = 0; i<10; i++)
{
en = new enemy();
en.y = Math.ceil(Math.random() * 290) + 10;
en.x = Math.ceil(Math.random() * 1000) + 500;
addChild(en);
}
}
And in the output window, this is what I get after mousing over 6 of the coins:
1
1
1
1
1
1
use:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class enemy extends MovieClip {
private var main:YourMainClass;
public function enemy(mc:MovieClip) { // if your main class extends MovieClip
main=mc;
// constructor code
this.addEventListener(Event.ENTER_FRAME, moveEnemys);
this.addEventListener(MouseEvent.MOUSE_OVER, disappear);
}
function disappear(event:MouseEvent)
{
this.visible = false;
mc.pointF();
}
function moveEnemys(event:Event)
{
this.x -= 6;
}
}
}
Here is where I add it to the stage in the main class:
var en:enemy;
private var points:int = 0;
function addEnemy()
{
for (var i:int = 0; i<10; i++)
{
en = new enemy(this);
en.y = Math.ceil(Math.random() * 290) + 10;
en.x = Math.ceil(Math.random() * 1000) + 500;
addChild(en);
}
}
public function pointF():void{
points++;
}
Thank you, but this didn't work. It gives me error 1118, implicit coercion of a value with a static type flash.display:MovieClip to a possibly unrelated type proFinal. My main class is named proFinal, and it does extend movie clip, so I'm not sure what's wrong.
It also gives me error 1061, call to a possibly undefined method pointF through a reference with a static type Class.
And another note, I tried something similar with a completely new class called point. It still didn't work, and I don't understand why, since it should be calling the method more than once.The important bits are bolded:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
public class enemy extends MovieClip {
var score:point;
var sound:Sound = new saw();
var soundchannel:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
public function enemy()
{
// constructor code
score = new point();
addEventListener(Event.ENTER_FRAME, moveEnemys);
addEventListener(MouseEvent.MOUSE_OVER, disappear);
}
function disappear(event:MouseEvent)
{
this.visible = false;
sound.play();
volumeAdjust.volume += 1;
soundchannel = sound.play();
soundchannel.soundTransform = volumeAdjust;
score.pointAdd();
}
function moveEnemys(event:Event)
{
this.x -= 6;
}
}
}
package {
import flash.display.MovieClip;
public class point extends MovieClip {
var points:uint = 0;
public function point()
{
}
public function pointAdd():void
{
points ++;
trace(points);
}
}
}
use:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
public class enemy extends MovieClip {
var sound:Sound = new saw();
var soundchannel:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
public function enemy()
{
// constructor code
addEventListener(Event.ENTER_FRAME, moveEnemys);
addEventListener(MouseEvent.MOUSE_OVER, disappear);
}
function disappear(event:MouseEvent)
{
this.visible = false;
sound.play();
volumeAdjust.volume += 1;
soundchannel = sound.play();
soundchannel.soundTransform = volumeAdjust;
point.pointAdd();
}
function moveEnemys(event:Event)
{
this.x -= 6;
}
}
}
package {
import flash.display.MovieClip;
public class point extends MovieClip {
var points:uint = 0;
public function point()
{
}
public static function pointAdd():void
{
points ++;
trace(points);
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class enemy extends MovieClip {
public function enemy()
{
// constructor code
addEventListener(Event.ENTER_FRAME, moveEnemys);
this.addEventListener(MouseEvent.MOUSE_OVER, disappear);
}
function disappear(event:MouseEvent)
{
this.visible = false;
point.pointAdd();
}
function moveEnemys(event:Event)
{
this.x -= 6;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
public class point extends MovieClip {
public function point()
{
}
public static function pointAdd():void
{
//it gave me an error unless I used the variable in this function
var points:uint;
points += 1;
trace(points);
}
}
}
North America
Europe, Middle East and Africa
Asia Pacific