Hi I have a character on the stage in my fla that when hitting a particle created in an a particle.as removes the particle. I have the code for removing the particle and also a score counter that adds one everytime a particle is removed (located in my particle.as). My problem is the score keeps resetting or I get sporadic results (not sure which) for example here I traced the results;
16
17
24
18
7
23
24
25
Heres my code for the particle.as if anyone could help me out I’d really appreciate it, i’ve commented on the relevant code. On the stage I have a dynamic text field called score_txt, that displays my score.
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Stage;
import flash.display.MovieClip;
import SimpleParticle;
public class SimpleParticleGenerator extends Sprite {
private var _speed:Number;
private var _angleDeg:Number;
private var _particles:Array;
/*score variable set to 0*/
var score:int = 0;
public function SimpleParticleGenerator() {
// start animating (frame based)
addEventListener(Event.ENTER_FRAME, update);
// start adding Particles (timer based)
var timer:Timer=new Timer(250, 0);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
_particles=new Array();
}
private function onTimer(event:TimerEvent):void {
for (var i:uint=0; i<2; i++) {
addParticle();
}
}
public function addParticle():void {
var shape:SimpleParticle=new SimpleParticle();
/*for random colour fills*/
/*shape.graphics.beginFill(Math.random()*0xFFFFFF, Math.random());*/
shape.graphics.beginFill(0xFFFFFF);
shape.graphics.drawCircle(0,0,10);
shape.graphics.endFill();
addChild(shape);
var scale:Number= Math.random() * 2;
var gravSpeed:Number=0;
var acc:Number=0;
var spread:Number=Math.random()*12;
spread-=0;
var angle:Number=(angleDeg+spread) / 180 * Math.PI;
var age:uint=0;
shape.gravSpeed=gravSpeed;
shape.acc=acc;
shape.speed=Math.random()*(speed*.2)+speed* 3;
shape.angle=angle;
shape.scale=scale;
shape.age=age;
shape.scaleX=shape.scaleY=scale;
_particles.push(shape);
}
public function update(event:Event):void {
var gravAcc:Number=0.8;
for (var i:uint=0; i<_particles.length; i++) {
var t=_particles*;
t.speed+=t.acc;
t.gravSpeed+=gravAcc;
var incx:Number=Math.cos(t.angle)*t.speed;
t.x+=incx;
t.y+=Math.sin(t.angle)*t.speed;
t.y+=Math.sin(90)*t.gravSpeed;
t.age++;
t.alpha=1-(t.age/30);
/*this is the hittest between character and particle*/
if(MovieClip(this.root).character_mc.hitTestObject (t)==true){
RemoveParticle(t);
continue;
}
if (t.age>30 || t.y>0) {
removeChild(t);
_particles*=null;
_particles.splice(i, 1);
}
}
}
/*here is the function that runs to remove the particle,*/
public function RemoveParticle(particle):void
{
this.removeChild(particle);
_particles.splice(_particles.indexOf(particle),1);
/*adds to the score variable and displays in - score_txt on the fla (stage)*/
score++;
MovieClip(this.root).score_txt.text = String(score);
trace (score)
}
public function set speed(n:Number):void {
_speed=n;
}
public function get speed():Number {
return _speed;
}
public function set angleDeg(n:Number):void {
_angleDeg=n;
}
public function get angleDeg():Number {
return _angleDeg;
}
}
}