ForumsProgramming ForumMouse Event Issue

2 3980
Perditus
offline
Perditus
8 posts
Nomad

This is the code for a function that generates people and adds them to the stage then animates, when you click on them they're supposed to go to a different frame. They did that at first, but as i added in the timers at the bottom to generate them continuously something made it the mouseEvent stop working. I'm sure there's a better way to do the timers though, but that works for my needs.

Anyway, would someone be able to tell me what's cancelling out the click function?

var peopleLagTime1:int = 6000;
var peopleLagTime2:int = 15000;

function generate() {
var classArray:Array = new Array();
for (var i=1; i<=1; i++) {
var _Class:Object = getDefinitionByName(&quoteopleMC&quot as Class;
var a:MovieClip = new _Class();
a.name = "a"+i;
classArray.push(_Class);
var randomInt = Math.floor(Math.random()*(501))+250;
var peopleTweenX = new Tween(a, "x", None.easeNone, 400, randomInt, 15, true);
var peopleTweenY = new Tween(a, "y", None.easeNone, 222.2, 1250, 15, true);
var peopleTweenW = new Tween(a, "width", None.easeNone, 2.18, 140.85, 15, true);
var peopleTweenH = new Tween(a, "height", None.easeNone, 9.03, 582.75, 15, true);
var frameInt = Math.floor(Math.random()*(2))+2;
stage.addChildAt(a, stripeIndex+1);
a.gotoAndStop(frameInt);
a.addEventListener(MouseEvent.CLICK, peopleDance);
function peopleDance(e:MouseEvent):void {
a.gotoAndStop(frameInt+2);
}
}
}
var n = 0;
n++;
var myTimer:Timer = new Timer(3000, 1000);
myTimer.addEventListener(TimerEvent.TIMER,spawn1);
function spawn1(event:TimerEvent) {
generate();
n++
}
myTimer.start();

var myTimer3:Timer = new Timer(15000, 1);
myTimer3.addEventListener(TimerEvent.TIMER,spawn2);
function spawn2(event:TimerEvent) {
var myTimer4:Timer = new Timer(2500, 1000);
myTimer4.addEventListener(TimerEvent.TIMER,spawn3);
function spawn3(event:TimerEvent) {
generate();
n++;
}
myTimer4.start();
}
myTimer3.start();

var myTimer5:Timer = new Timer(30000, 1);
myTimer5.addEventListener(TimerEvent.TIMER,spawn4);
function spawn4(event:TimerEvent) {
var myTimer6:Timer = new Timer(1500, 1000);
myTimer6.addEventListener(TimerEvent.TIMER, spawn5);
function spawn5(event:TimerEvent) {
generate();
n++;
}
myTimer6.start();
}
myTimer5.start();

var person = "a"+n;

var myTimer2:Timer = new Timer(peopleLagTime2, 1000);
myTimer2.addEventListener(TimerEvent.TIMER,removeA);
function removeA(event:TimerEvent) {

}

  • 2 Replies
PixelSmash
offline
PixelSmash
566 posts
Nomad

I think the problem lies in your function. I don't know if it's possible, but at least it's highly unpractical to have functions within functions (your generate function contains the peopleDance function).
Either way, if you decouple those two, you'll run into a null reference error, since the standalone function peopleDance doesn't know what 'a' is.

To solve this, you can use event.currentTarget, which will be a reference to the a dispatching the event. Cast it to a MovieClip, then work your magic
(ps. You'll need to put the variable frameInt into your peopleDance function then as well, of course!)

Good luck!

Perditus
offline
Perditus
8 posts
Nomad

Thank you PixelSmash, but I just found out the problem and it was the index. By removing the index it solved the problem.

Showing 1-2 of 2