# Nothing wrong with the code I just need an explanation

**URL:** <https://forum.kirupa.com/t/nothing-wrong-with-the-code-i-just-need-an-explanation/267512>\
**Category:** flash\
**Created:** [August 2, 2008, 1:58am UTC](https://forum.kirupa.com/t/nothing-wrong-with-the-code-i-just-need-an-explanation/267512 "2008-08-02T01:58:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)\
**Post date:** [August 2, 2008, 1:58am UTC](https://forum.kirupa.com/t/nothing-wrong-with-the-code-i-just-need-an-explanation/267512/1 "2008-08-02T01:58:41Z")

</div>

[COLOR=“DarkRed”]Hi,

This is a simple (simple?) particle system that came in the Learning ActionScript 3.0 book.

I was trying to create a particle system and than I thought could use this as a reference but I am lost.

I am confused, why is that without having numbers assigned to the variables \_xpos, \_ypos etc. it is working fine? Could someone explain this code, please?

Any help would be appreciated.

thanks,  
fs\_tigre[/COLOR]

package {

```
import flash.display.Sprite;
import flash.events.Event;

public class ParticleDemo extends Sprite {

	public var emitterX:Number = stage.stageWidth/2;
	public var emitterY:Number = stage.stageHeight/2;

	public function ParticleDemo() {
		stage.addEventListener(Event.ENTER_FRAME,onLoop, false, 0, true);
	}
	
	private function onLoop(evt:Event):void {
		var p:Particle = new Particle(emitterX,
									  emitterY,
									  Math.random()*11 - 6,
									  Math.random()*-20,
									  1,
									  Math.random()*0xFFFFFF);
		addChild(p);
	}
	
}

```

}

[SIZE=“4”]**[COLOR=“Black”][COLOR=“DarkRed”]This is the particle classs[/COLOR][/COLOR]**[/SIZE]

package{

```
import flash.display.*;
import flash.geom.*
import flash.events.Event;

public class Particle extends Sprite {
	
	private var _xpos:Number;
	private var _ypos:Number;
	private var _xvel:Number;
	private var _yvel:Number;
	private var _grav:Number;

	public function Particle(xp:Number, yp:Number, xvel:Number, yvel:Number, grav:Number, col:uint) {
		_xpos = xp;
		_ypos = yp;
		_xvel = xvel
		_yvel = yvel
		_grav = grav;
		
		var ball:Sprite = new Ball();
		addChild(ball);
		
		x = _xpos;
		y = _ypos;
		alpha = .8;
		scaleX = scaleY = Math.random() * 1.9 + .1;
		
		var colorInfo:ColorTransform = ball.transform.colorTransform;
		colorInfo.color = col;
		ball.transform.colorTransform = colorInfo;
		
		addEventListener(Event.ENTER_FRAME, onRun, false, 0, true);
	}
	
	private function onRun(evt:Event):void {
		_yvel += _grav;
		_xpos += _xvel;
		_ypos += _yvel;
		x = _xpos;
		y = _ypos;
		if (_xpos &lt; 0 || _ypos &lt; 0 || _xpos &gt; stage.stageWidth || _ypos &gt; stage.stageHeight) {
		   removeEventListener(Event.ENTER_FRAME, onRun);
		   parent.removeChild(this);
		}
	}
}

```

}
