Reaching variables in other functions - actionscript-3

Im making a game as a graduation project and have encountered a little issue. The goal of the game is to maneuver a ship and avoid asteroids as long as possible. I'm almost done but I'm now trying to solve the collision detection.
My problem is how do I reach the enemy in the loop variable so that it can check for collision with it? Here is the code:
package com.asgamer.basics1
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class Engine extends MovieClip
{
private var numStars:int = 80;
private static var enemyList:Array = new Array();
private var ourShip:Ship;
public function Engine() : void
{
ourShip = new Ship(stage);
ourShip.x = stage.stageWidth / 2;
ourShip.y = stage.stageHeight / 2;
stage.addChild(ourShip);
for (var i:int = 0; i < numStars; i++)
{
stage.addChildAt(new Star(stage), 1)
}
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, krash);
}
private function krash(e:Event) : void
{
function krash(e:Event):void
{
if (enemy.hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
private function loop(e:Event) : void
{
if (Math.floor(Math.random() * 10) == 5)
{
var enemy:Asteroid = new Asteroid(stage);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, removeEnemy, false, 0, true);
enemyList.push(enemy);
stage.addChild(enemy);
}
}
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
}
}
}
As you can see I have a function for making enemies which is called loop, and a function for collision detection called krash. But since enemy is a variable inside loop, how can I check for collision with it in the krash function. (Sort of new to actionscript, so I dont know the terminology all to well)

In krash you will need to check each enemy in enemyList.
example:
private function krash(e:Event) : void
{
function krash(e:Event):void
{
for(var i:int = 0; i < enemyList.length; i++)
{
if (enemyList[i].hitTestObject(ourShip)==true)
{
stage.removeChild(ourShip);
stage.removeEventListener(Event.ENTER_FRAME, krash);
}
}
}
}
Unfortunately, if you have a lot of enemies on the stage the program will lag a bit because you're checking for collisions with all of them every frame.
Also, I'd reccommend you add something to remove you enemies from the screen :P
private function removeEnemy(e:Event)
{
enemyList.splice(enemyList.indexOf(e.currentTarget), 1);
stage.removeChild(enemyList.indexOf(e.currentTarget));
}
Hope this helps.

Related

How can I stop a colliding object from continuing falling after several seconds (AS3)?

I have a problem that is related to AS3, it involves two code locations (MainTimeline and an MC-extending class), as well as a basic physics engine function. The problem is that when the objects collide (enemy and ground), they stay collided, but after a short period of time (2-6 seconds), the object that is gravitated teleports under the object it collided with, and continues to fall.
Thank you for any assistance.
P.S. I do not prefer document class solutions.
Timeline code:
`
stop();
var ground: MovieClip;
var enemy:Enemy = new Enemy();
stage.addChild(enemy);
enemy.x = 400;
enemy.y = 150;
enemy.rotation = 0;
enemy.addEventListener("enterFrame", collisions);
function collisions(e: Event): void {
var collision: Boolean = false;
if (ground.hitTestObject(enemy)) {
collision = true;
}
if (collision) {
while (collision) {
enemy.y -= 0.1;
collision = false;
if (ground.hitTestObject(enemy)) {
collision = true;
}
}
Class(Enemy).yVel = 0;
}
}
Class code:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Enemy extends MovieClip {
var xVel: int;
var yVel: int;
public function Enemy() {
// constructor code
addEventListener("enterFrame", onEnterFrame);
}
function onEnterFrame(e: Event): void {
yVel += 2;
this.x = xVel;
this.y = yVel;
}
}
}

how to reference a hit test object between .as files [AS3]

i'm trying to make a top down shooter game, and have been following tutorials here: http://gamedev.michaeljameswilliams.com/2008/09/17/avoider-game-tutorial-1/
and here: as3gametuts.com/2013/07/10/top-down-rpg-shooter-4-shooting/
i've managed to get shooting and movement, but i need to get a hit test object to register when the bullet (defined in its own seperate as class file) and the enemy (also defined in seperate file) come into contact. code below:
Enemy code:
package
{
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
public function Enemy()
{
x = 100;
y = -15;
}
public function moveDownABit():void
{
y = y + 3;
}
}
}
Bullet code:
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Bullet extends MovieClip
{
private var stageRef:Stage;
private var speed:Number = 10;
private var xVel:Number = 0;
private var yVel:Number = 0;
private var rotationInRadians = 0;
public var enemy:Enemy;
public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void
{
this.stageRef = stageRef;
this.x = X;
this.y = Y;
this.rotation = rotationInDegrees;
this.rotationInRadians = rotationInDegrees * Math.PI / 180;
}
public function bullethit():void{
if (Bullet.hitTestObject(enemy)){
gameTimer.stop();
}
}
public function loop():void
{
xVel = Math.cos(rotationInRadians) * speed;
yVel = Math.sin(rotationInRadians) * speed;
x += xVel;
y += yVel;
if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
{
this.parent.removeChild(this);
}
}
}
}
Main.as document class code:
package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip
{
public var player:Player;
public var bulletList:Array = []; //new array for the bullets
public var enemy:Enemy;
public var gameTimer:Timer;
public function Main():void
{
player = new Player(stage, 320, 240);
stage.addChild(player);
enemy = new Enemy();
addChild( enemy );
gameTimer = new Timer( 25 );
gameTimer.addEventListener( TimerEvent.TIMER, moveEnemy );
gameTimer.start();
stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true);
stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true); //add an EventListener for the loop
}
public function moveEnemy( timerEvent:TimerEvent ):void
{
enemy.moveDownABit();
}
public function loop(e:Event):void //create the loop function
{
if(bulletList.length > 0) //if there are any bullets in the bullet list
{
for(var i:int = bulletList.length-1; i >= 0; i--) //for each one
{
bulletList[i].loop(); //call its loop() function
}
}
}
public function shootBullet(e:MouseEvent):void
{
var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation);
bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true); //triggers the "bulletRemoved()" function whenever this bullet is removed from the stage
bulletList.push(bullet); //add this bullet to the bulletList array
stage.addChild(bullet);
}
public function bulletRemoved(e:Event):void
{
e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved); //remove the event listener so we don't get any errors
bulletList.splice(bulletList.indexOf(e.currentTarget),1); //remove this bullet from the bulletList array
}
}
}
As Vesper said, you'll want to do your checks in the Main class. You've already got a game loop set up, so you can just add the check in there:
public function loop(e:Event):void //create the loop function
{
if(bulletList.length > 0) //if there are any bullets in the bullet list
{
for(var i:int = bulletList.length-1; i >= 0; i--) //for each one
{
bulletList[i].loop(); //call its loop() function
// check to see if the enemy has been hit
if(enemy.hitTestObject(bulletList[i]))
{
// the enemy has been hit by the bullet at index i
}
}
}
}
Since you currently only have a single enemy, you're just testing each bullet against that one enemy. If you had more enemies, you'd want to keep an array of references to those enemies and do a nested loop, checking to see if any of the enemies were hit by any of the bullets.

got Error #1009 in flash as3

i'm new in flash and as3 programming, and this is my first project. i found error on my project like this
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src.char::Enemy/Remove()
at src.screen::Gameplay/Collision()
at src.screen::Gameplay/Routine()
I think the error occurs because there is no function Remove () on the gameplay, but I'm not sure that's true. here's the enemy.as
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Enemy extends MovieClip {
private var timer:Timer = new Timer(25);
public function Enemy(xPos:Number, yPos:Number) {
x = xPos;
y = yPos;
timer.addEventListener(TimerEvent.TIMER, MoveDown);
timer.start();
}
private function MoveDown(e:TimerEvent):void {
y += 3;
if (y>400) {
Remove();
}
}
public function Remove():void {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, MoveDown);
parent.removeChild(this);
}
}
and here's the the gameplay.as
public class Gameplay extends MovieClip {
private var timer:Timer = new Timer(500);
private var player:Player;
public function Gameplay() {
addEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
addEventListener(Event.ENTER_FRAME, Routine);
gameplayBack.addEventListener(MouseEvent.CLICK, GoToMap);
timer.addEventListener(TimerEvent.TIMER, OnTick);
timer.start();
InitPlayer();
InitLifePoint();
}
private function InitLifePoint():void {
lifePoint.gotoAndStop(1);
}
private function Routine(e:Event):void {
Collision();
}
private function Collision():void {
for (var i:int = 0; i < enemies.length; i++ ) {
if (player.hitTestObject(enemies[i])) {
PlayerHit();
enemies[i].Remove();
return;
}else {
for (var j:int = 0; j < bullets.length; j++ ) {
if (bullets[j].hitTestObject(enemies[i])) {
layerParticle.addChild(new Blast(bullets[j].x, bullets[j].y));
layerParticle.addChild(new Smoke(bullets[j].x, bullets[j].y));
bullets[j].Remove();
enemies[i].Remove();
scorePlay.text = int(scorePlay.text) + 10 + "";
trace(scorePlay.text);
return;
}
}
}
}
}
private var life:int = 1000;
private var currentLife:int = 1000;
private function PlayerHit():void {
currentLife -= 100;
if (currentLife <= 0) {
lifePoint.gotoAndStop(100);
GameOver();
}else {
lifePoint.gotoAndStop(100 - currentLife / life * 100);
}
}
private var result:Result = new Result();
private function GameOver():void {
result.youWin.alpha = 0;
result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
result.x = 0;
result.y = 0;
addChild(result);
}
private function InitKeyboard(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, InitKeyboard);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
}
private function KeyDown(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.LEFT: MoveLeft(); break;
case Keyboard.RIGHT: MoveRight(); break;
case Keyboard.SPACE: Fire(); break;
}
}
private var bullets:Array = new Array();
private function Fire():void {
var bullet:Bullet = new Bullet(player.x, player.y);
bullet.scaleX = 0.25;
bullet.scaleY = 0.25;
bullet.addEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
layerParticle.addChild(bullet);
bullets.push(bullet);
}
private function RemoveBulletArray(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, RemoveBulletArray);
var index:int = bullets.indexOf(Bullet(e.currentTarget), 0);
bullets.splice(index, 1);
}
private function MoveRight():void {
if (player.x < 550) {
player.x += 5;
}
}
private function MoveLeft():void {
if (player.x > 0) {
player.x -= 5;
}
}
private function InitPlayer():void {
player = new Player(550 * 0.5, 350);
layerChar.addChild(player);
}
private function OnTick(e:TimerEvent):void {
RandomEnemy();
}
private var enemies:Array = new Array();
private function RandomEnemy():void {
var enemy:Enemy = new Enemy(Math.random() * 550, 0);
enemy.addEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
layerChar.addChild(enemy);
enemies.push(enemy);
}
private var remaining:int = 10;
private function RemoveFromArray(e:Event):void {
removeEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
var index:int = enemies.indexOf(Enemy(e.currentTarget), 0);
enemies.slice(index, 1);
remaining--;
if (remaining == 0) GameWin();
}
private function GameWin():void {
result.youLose.alpha = 0;
result.score.text = scorePlay.text;
result.ok.addEventListener(MouseEvent.CLICK, GoToMap);
result.x = 0;
result.y = 0;
addChild(result);
}
private function GoToMap(e:MouseEvent):void {
dispatchEvent(new ScreenEvent(ScreenEvent.MAP));
}
}
Your problem is a NPE (Null Pointer Exception/Error) inside the Enemy.Remove() method:
public function Remove():void {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, MoveDown);
parent.removeChild(this);
}
Either your timer property is null (which I doubt, looking at your code) or the parent property are.
In a MovieClip the parent property are filled with a DisplayObject if your MovieClip is added to it, if not, this property is null.
Your problem probably is that you are removing (from its parent) this MovieClip more than once, or is trying to remove it without adding it first.
To make sure this is the problem, add a if statement to check the parent property first, like this:
if(parent != null)
{
parent.removeChild(this);
}
Note:
This may solve your NPE problem, but will not solve what is causing it, which may lead you into more and more inexplicable bugs.
Double check your logic to make sure you're removing a previously added MovieClip, or that you aren't removing it more than once.
Do you notice the flaw in the Collision function, if you observe like this:
for (var i:int = 0; i < enemies.length; i++) {
if (~) {
...
enemies[i].Remove();
...
} else {
for (~) {
if (~) {
...
enemies[i].Remove();
...
}
}
}
}
Apparently, in the second for loop you could be easily referencing the same Enemy Object.
And the problem comes after you call Remove function, because by doing parent.removeChild(this); you remove the only reference of the object to it's parent object.
You can try to do one of these:
Keep a reference to the parent object in the class, manually.
Keep a state variable in the class to check if it is to be a part of display list or not.
Move the enemies[i].Remove(); code into outermost loop.
If possile, recycle the object. Especially, when you are just
moving (x,y) the movieclip around.

How to remove all of one class of children from screen in Flash CS4 AS3?

Ok, so I've been stuck on this for about three hours now and I finally feel like asking for help.
Basically, I am trying to remove all instances of an enemy object from the screen when my player ship makes contact with one of them, as he then loses a life and is put back into a safe position on the screen.
EDIT: This is all the code from my Enemy Dude .as file, a bit overboard maybe but nonetheless.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
public class Enemydude extends MovieClip
{
private var _root:Object;
private var speed:int = 6;
private var shipps = this
public function Enemydude()
{
addEventListener(Event.ADDED, beginclass);
addEventListener(Event.ENTER_FRAME, entFrame);
}
private function beginclass(event:Event):void
{
_root = MovieClip(root);
}
private function entFrame(event:Event):void
{
x -= speed;
if(this.x < -64)
{
removeEventListener(Event.ENTER_FRAME, entFrame);
_root.removeChild(this)
}
if(_root.gameover)
{
x = -700
removeEventListener(Event.ENTER_FRAME, entFrame);
removeEventListener(Event.ADDED, beginclass);
}
for (var i:int = 0; i<_root.playerBulletContainer.numChildren; i++)
{
var bulletTarget:MovieClip = _root.playerBulletContainer.getChildAt(i)
if (hitTestObject(bulletTarget))
{
removeEventListener(Event.ENTER_FRAME, entFrame);
_root.removeChild(this);
_root.playerBulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
_root.Score += 10
makeExplosion();
}
}
if(hitTestObject(_root.mcship))
{
makeExplosion();
shipPos();
removethis();
}
}
private function makeExplosion()
{
var sndExplode:snd_explosion1;
var sndExplodeChannel:SoundChannel;
sndExplode=new snd_explosion1();
sndExplodeChannel=sndExplode.play();
var newExplosion01:explosionEffect=new explosionEffect ;
newExplosion01.x=this.x;
newExplosion01.y=this.y;
_root.explosionContainer.addChild(newExplosion01);
}
private function shipPos()
{
_root.lives -= 1;
_root.mcship.x = 80;
_root.mcship.y = 225;
for each(var i:Enemydude in _root.enemies)
{
removethis();
}
_root.enemies.length = 0;
}
public function removethis():void
{
if(parent) parent.removeChild(this)
removeEventListener(Event.ENTER_FRAME, entFrame);
}
}
}
EDIT: And this is the code I now have that relates to the Enemydude in my main timeline, quite sorry about all this.
var enemies:Array = [];
var Shipheight:Number = 300;
var Enemytime:int = 0;
var Enemylimit:int = 16;
if (Enemytime<Enemylimit)
{
Enemytime ++;
}
else
{
var newEnemy01 = new Enemydude();
newEnemy01.y = Shipheight;
newEnemy01.x = stage.stageWidth + 64;
addChild(newEnemy01);
enemies.push(newEnemy01);
Enemytime = 0
function shipY(event:Event):void
{
Shipheight = Math.ceil(Math.random()* 250) + 80;
}
Thank you for your help in advance, any advice is appreciated.
I suggest storing your enemies in an Array.
For example, create the array enemies:
var enemies:Array = [];
And then amend your code to:
else
{
var newEnemy01 = new Enemydude();
newEnemy01.y = Shipheight;
newEnemy01.x = stage.stageWidth + 64;
addChild(newEnemy01);
enemies.push(newEnemy01);
Enemytime = 0;
}
That way you can remove all of the enemies using this new array:
for each(var i:Enemydude in enemies)
{
i.remove(); // Or whatever function Enemydude has to remove itself.
}
// Empty the enemies Array.
enemies.length = 0;
Here's the .remove() method you could make for Enemydude:
public function remove():void
{
if(parent) parent.removeChild(this);
// Remove any event listeners etc from this object.
}
A common and easy way of doing this is to create a subcontainer to hold the objects and destroy this object instead. It makes easy for some collision checks too, since you can use the holder object to make one check against the player.
If you don't want to create this, you can use an array or a vector to store references to this objects, what makes easy to traverse the list.
I persoally recommend the vector aprouch:
var enemyList:Vector.<Enemy> = new Vector.<Enemy>();
Then you can loop almost like an array (as Marty Wallace showed on his answer):
for each(var e:Enemy in enemyList) {
container.removeChild(e);
}
enemyList.length = 0; // empty vector
Vectors are a bit slower than normal arrays, but are type safe. The difference in performance is almost negligible in most cases.

A loop in enterframe?

I'm animating a bunch of words in AS3. Because I'm going to be using this on a mobile device, I want to use bitmaps rather than Sprites. So I've created WordObjects, which have a .bitmap property that I can access.
I have the following code, which fires on the click event and loops through an array inside an enterframe event. This is probably a bad idea, but I'm not sure how to do it better. (What is surprising is that it runs just fine in Flashbuilder, but slows to a crawl in Flash CS5.)
Is there some better way to do this? I just want an efficient way to animate the array of bitmaps.
private function clickhandler (e:MouseEvent){
this.addEventListener(Event.ENTER_FRAME, blowemup);
}
private function blowemup(e:Event){
var newPosition:Number;
for(var i:int=0; i<arrWordObjects.length; i++)
{
newPosition = updatePosition(arrWordObjects[i].bitmap);
arrWordObjects[i].bitmap.x += newPosition;
arrWordObjects[i].bitmap.y += getRandomNumber();
}
}
Something that will make a huge difference is using for each(Object in Array) rather than the standard for loop.
private function blowemup(e:Event):void
{
var newPosition:Number;
var i:ArrWordsObjectClass; // <-- don't know what the class for this is, just replace
for each(i in arrWordObjects)
{
newPosition = updatePosition(i.bitmap);
i.bitmap.x += newPosition;
i.bitmap.y += getRandomNumber();
}
}
A for each loop is typed, meaning a lot of time is saved where normally it'd be trying to work out what arrWordObjects[i] is every iteration.
Also, side note: using one ENTER_FRAME driven function and looping through everything in your application that you want to handle each frame is much more efficient than applying hundreds of listeners for objects.
I normally create a handler class that contains the ENTER_FRAME and an array storing my objects, like so:
package
{
import flash.events.Event;
import flash.display.Sprite;
public class Handler extends Sprite
{
// vars
public var elements:Array = [];
/**
* Constructor
*/
public function Handler()
{
addEventListener(Event.ENTER_FRAME, _handle);
}
/**
* Called on each dispatch of Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
var i:Element;
for each(i in elements)
{
i.step();
}
}
}
}
Then I create a base class for all the objects that I want to handle, containing the step() function called above.
package
{
import flash.display.DisplayObject;
public class Element extends Object
{
// vars
public var skin:DisplayObject;
/**
* Called on each dispatch of Event.ENTER_FRAME at Handler
*/
public function step():void
{
// override me
}
}
}
Now just extend Element with your objects:
package
{
import flash.display.Sprite;
public class MyThing extends Element
{
/**
* Constructor
*/
public function MyThing()
{
skin = new Sprite();
skin.graphics.beginFill(0);
skin.graphics.drawCircle(0,0,40);
skin.graphics.endFill();
}
/**
* Override step
*/
override public function step():void
{
skin.x += 4;
}
}
}
And get it all going!:
var handler:Handler = new Handler();
var m:MyThing;
var i:uint = 0;
for(i; i<10; i++)
{
m = new MyThing();
m.y = Math.random()*stage.stageHeight;
handler.elements.push(m);
addChild(m.skin);
}
How many bitmaps do you plan to have on the stage at a time?
I have had 40 900x16px bitmaps animating on the stage at full speed running on my iphone using air 2.6.
I used a foreach loop in an enterframe event which i added on mouseclick and removed once the animation was finished.
Remember to compile it for the mobile with gpu rendering enabled. (gpu in your app.xml if you are using air 2.6)
This is worth a read too, it explains a lot about performance for mobile devices
http://help.adobe.com/en_US/as3/mobile/WS901d38e593cd1bac-3d719af412b2b394529-8000.html
Here is a basic example of what I had...
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(frameRate="30", backgroundColor="#FF00FF")]
public class Test extends Sprite
{
private var fields:Vector.<Bitmap> = new Vector.<Bitmap>();
public function Test()
{
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
for(var i:int = 0; i< 37; i++){
var bd:BitmapData = new BitmapData(960, 16, true, 0x000000);
bd.fillRect(new Rectangle(0, 0, 900, 16), Math.round( Math.random()*0xFFFFFFFF ));
var b:Bitmap = new Bitmap(bd);
b.x = 0;
b.y = i*16;
stage.addChild(b);
fields.push(b);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private var inertia:Boolean = false;
private var yCurrent:Number;
private var ySpeed:Number;
private var startY:Number;
private var cy:Number = 0;
private function onEnterFrame(e:Event):void{
if(!inertia){
ySpeed = (startY - yCurrent) ; // / 16;
startY = yCurrent
} else {
ySpeed *= 0.8;
if(ySpeed < 0.01 && ySpeed > -0.01){
inertia = false;
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
cy += ySpeed;
if(cy > 640)
cy -= 640;
var ty:Number = cy;
for each(var tf:Bitmap in fields){
tf.y = ty;
ty += 16;
if(ty > 640)
ty -= 640;
}
}
private function onMouseDown(e:MouseEvent):void{
inertia = false;
startY = e.stageY;
yCurrent = e.stageY;
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseMove(e:MouseEvent):void{
yCurrent = e.stageY;
}
private function onMouseUp(e:Event):void{
inertia = true;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
}
I would suggest looking at writing a custom effect on Adobe's website over registering for ENTER_FRAME event. What you've put up there means this code will forever run as long as the program is running. If you wanted to stop the effect or run for 10 frames and stop then you'll have to write more code. It gets even more complex if you want to apply this to several instances. You're going to have to resolve problems that custom effects framework solves.
I'd read how to write custom effects here:
http://livedocs.adobe.com/flex/3/html/help.html?content=createeffects_1.html