I am building an Adobe Air AS3 IOS and Android App, in which i have a movie clip in the center of the stage. When you start touching this movie clip, you can move it all around the stage.
This is how i'm doing so :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
MC_M1.alpha = 1;
MC_M1.addEventListener(Event.ENTER_FRAME, ifHitAct);
MC_M1.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
MC_M1.x = 0.516 * gameIntro.stageWidthToUse;
MC_M1.y = 0.75 * gameIntro.stageHeightToUse;
MC_M1.height = 0.2 * gameIntro.stageHeightToUse;
MC_M1.width = MC_M1.height / 1.4;
gameIntro.STAGE.stage.addChildAt(MC_M1,1);
function onTouchBegin(event:TouchEvent)
{
trace("TouchBegin");
if (touchMoveID != 0)
{
trace("It Did Not");
return;
}
touchMoveID = event.touchPointID;
gameIntro.STAGE.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
gameIntro.STAGE.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
function onTouchMove(event:TouchEvent)
{
if (event.touchPointID != touchMoveID)
{
return;
}
//trace("Moving")
MC_M1.x = event.stageX;
MC_M1.y = event.stageY;
}
function onTouchEnd(event:TouchEvent)
{
if (event.touchPointID != touchMoveID)
{
return;
}
//trace("Ending");
touchMoveID = 0;
gameIntro.STAGE.stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
gameIntro.STAGE.stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
}
When the player actually looses the game, what i am actually doing is the following :
MC_M1.removeEventListener(Event.ENTER_FRAME , ifHitAct);
MC_M1.removeEventListener(TouchEvent.TOUCH_BEGIN , onTouchBegin);
gameIntro.STAGE.stage.removeChild(MC_M1);
MC_M1.alpha = 0;
isDead = 1;
replayButToUse.x = 0.127 * gameIntro.stageWidthToUse;
replayButToUse.y = 0.91 * gameIntro.stageHeightToUse;
replayButToUse.addEventListener(MouseEvent.CLICK, gotoIntro);
This is all happening in a class called : introClassToUse.
So when the users looses, he will get a replay button, and when he clicks it, he will go back to the same class and reload everything, using the following code :
function gotoIntro(event:MouseEvent):void
{
replayButToUse.removeEventListener(MouseEvent.CLICK, gotoIntro);
replayButToUse.alpha = 0;
replayButToUse.removeEventListener(MouseEvent.CLICK, gotoIntro);
stop();
var reload:introClassToUse = new introClassToUse();
}
And so everything loads back up and the game restarts. My problem is, i'm facing a very weird behavior when i tend to replay the game more than 2-3 times. The MC_M1 just stops listening to any touch event, but keeps on listening to ENTER_FRAME events, in which i keep touching the MC_M1 but it seems to not respond to it. I even debugged it remotely from my iPhone, for the first couple of replays, i can see the trace("TouchBegin"); with it's outcome, it was showing me TouchBegin, but after a few replays, the touch events just froze. What am i missing?
Any help is really appreciated, i'm new in AS3, i need to learn so i could manage more
Edit 1 :
I have no code on any frame, i just have lots of AS Classes.
The fla file is linked to an AS Class called gameIntro. In this class, i have linked the following :
- STAGE is an object of type Stage.
- gameIntro.STAGE = stage
Later on, when the user clicks a play button, i call the class introClassToUse. This class has all the game functionalities. All the code present above is in introClassToUse. When the user looses and clicks the replay button, he will go to "goToIntro" function, im which i recall the introClassToUse.
It's all working fine, with several other timers implemented and all, the only problem is that after several replays, the MC_M1 just freezes over
I am removing the MC_M1 each time the user looses and re-add them when i call back the introClassToUse, because i tried to use the .visible property, it didn't work at all ( this is why i am using the gameIntro.STAGE.stage.removeChild(MC_M1)
I know the question is old but maybe someone is still wondering what is going on here (like me).
There are lot of problems in you code but I thing the root of your problem starts here:
function gotoIntro(event:MouseEvent):void{
//...
var reload:introClassToUse = new introClassToUse();
}
It is usually unwanted behavior if simply creating an instance does more than nothing to your program and you don't even need to assign it to variable in this case.
You mentioned this code is located in your introClassToUse class. This basically means that you are creating new instance of your game inside old one and this seem to be completely awry.
You should consider using only instance properties in your class definition and create new introClassToUse() in external classes;
You didn't include many important details about your code like
How the whole class structures look like - for example you can't place line like MC_M1.addEventListener(Event.ENTER_FRAME, ifHitAct);in the scope of your class so obviously you have this in some function and we don't know when and from where it is called.
Where and how your variables are declared, and assigned. It's hard to tell if your MC_M1 is property of an instance or a class, is it internal/public/private/...
Do you link library symbols to your classes or acquire it from stage.
There could be many things that could give you such result. Based on what you wrote I've reproduced behavior similar to what you've describe but using mouse event and a dummy loose condition. This ends the game each time you drop the mc partially outside right edge of the sage, show restart button and starts again if you click it (basically it's mostly your code). It works fine for about 10s and than suddely you can't move the mc anymore. The frame event is still tracing out but touch/mouse is not.
How can it be? I suspect that you could remove only listeners somewhere and have invisible mc stuck on the new one. And this could be easy overlooked, especially if you using static properties. Again we don't even know where is your movie clip coming from so we can only guess what is happening whit your code but I've tried to take the example simple this is how I did it. The problem may lay in some completely different place but you can guess for all scenarios.
Document class of the project - GameIntro.as
package
{
import flash.display.Sprite;
public class GameIntro extends Sprite
{
//Document class. this need to be compiled with strict mode off.
public function GameIntro() {
GameIntro.STAGE = stage;
GameIntro.stageWidthToUse = stage.stageWidth;
GameIntro.stageHeightToUse = stage.stageHeight;
var intro:IntroClassToUse = new IntroClassToUse();
stage.addChild(intro);
}
}
}
IntroClassToUse.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* You need to have library symbol linked to this class in .fla with two mcs -
* mcFromLibrarySymbol (dragable) and repButton (reapatButton)
*/
public class IntroClassToUse extends MovieClip
{
var t = 0; //timer ticks
var fc:uint = 0; //frames counter
var isDead = 0;
var mc;
static var repButton;
var logicContex:Timer = new Timer(30);
public function IntroClassToUse() {
trace("toUse", GameIntro.stageWidthToUse);
mc = mcFromLibrarySymbol;
if(!repButton) repButton = repButtonX;
logicContex.addEventListener(TimerEvent.TIMER, logicInterval);
logicContex.start();
init();
}
internal function init() {
trace("init");
mc.alpha = 1;
mc.addEventListener(Event.ENTER_FRAME, onFrame);
mc.addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
mc.x = 0.516 * GameIntro.stageWidthToUse;
mc.y = 0.75 * GameIntro.stageHeightToUse;
mc.height = 0.2 * GameIntro.stageHeightToUse;
mc.width = mc.height / 1.4;
GameIntro.STAGE.stage.addChildAt(mc, 1);
}
internal function onLoose() {
trace("onLoose");
mc.removeEventListener(Event.ENTER_FRAME , onFrame);
mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
GameIntro.STAGE.stage.removeChild(mc);
mc.alpha = 0;
isDead = 1;
repButton.x = 0.127 * GameIntro.stageWidthToUse;
repButton.y = 0.91 * GameIntro.stageHeightToUse;
repButton.addEventListener(MouseEvent.CLICK, onReplay);
repButton.alpha = 1;
}
internal function onReplay(e:MouseEvent):void {
trace("onReplay");
repButton.removeEventListener(MouseEvent.CLICK, onReplay);
repButton.alpha = 0;
stop();
new IntroClassToUse();
}
internal function onMDown(e:MouseEvent):void {
trace("mouseDow");
GameIntro.STAGE.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMMove);
GameIntro.STAGE.stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
}
internal function onMMove(e:MouseEvent):void {
mc.x = e.stageX;
mc.y = e.stageY;
}
//you loose the game if you release you mc with part of it over rigth stage edge.
internal function onMUp(e:MouseEvent):void {
trace("mouseUp");
GameIntro.STAGE.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
GameIntro.STAGE.stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
trace("Stage:", GameIntro.STAGE.numChildren);
if (mc.x + mc.width > GameIntro.STAGE.stageWidth) onLoose();
}
internal function onFrame(e:Event):void {
trace("frames", fc++);
}
internal function logicInterval(e:TimerEvent):void {
if (t++ < 300 || !isDead) return;
init();
mc.alpha = 0;
mc.removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
isDead = 0;
}
}
}
Related
My app is very slow on mobile devices.
It uses a lot of Event.ENTER_FRAME event listeners, so I switched (as I read they are much more performance heavy) to a global timer that these classes listen for if they need to update (they only use these timers if they have been initiated by a TouchEvent).
I also tried enabling hardware acceleration, and switching to using the CPU, but these have not helped with the lag issues to a noticable amount. (my app has very few animations so I think this is the best decision)
I do have a lot of MovieClips in it, however very few of them actually animate, so I doubt this is an issue. (This is also why I do not freeze and unfreeze objects, as they are never removed from the stage)
On this website they said the following about timers
– the more timer instances linger in the system, the more you can expect slow and choppy gameplay or unresponsive UI.
I only use one timer, so I didn't think this would be an issue, but I do have a lot of event listeners that use this timer to update, so I can only figure that the timer must be the issue. Thus to avoid lag I am looking for a better alternative.
To be clear, I'm not using many Event.ENTER_FRAME event listeners anymore, I've mostly switched to timers. With that being said, here is an example of a more resource demanding one.
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class mc_BattleScene extends MovieClip
{
public static var mc_PlayerFace:MovieClip;
public static var enemyAttackEffect:MovieClip;
public static var mc_playerHitBox:MovieClip;
public static var battler1:Number = 1;
public static var battler2:Number = 1;
public static var battler3:Number = 1;
public var lvlModifier:Number;
public var dmgReduction:Number;
public var dmgDealt:Number;
public static var dmgSpeed:Number = 1;
public function mc_BattleScene()
{
visible = false;
addEventListener(Event.ENTER_FRAME, onFrame);
mc_PlayerFace = playerFace_mc;
enemyAttackEffect = attackEffect_mc;
mc_playerHitBox = playerHitBox_mc;
function onFrame(e:Event):void
{
battler1_mc.gotoAndStop(battler1);
battler2_mc.gotoAndStop(battler2);
battler3_mc.gotoAndStop(battler3);
if (Game.playerInteractionStatus[1])
{
//we are fighting
visible = true;
if (attackEffect_mc.hitTestObject(playerHitBox_mc))
{
// attack hit us, deal dmg
dmgReduction = (Game.playerStats[2] * (Game.enemyStats[1]));
dmgDealt = Game.enemyStats[1] - dmgReduction;
attackEffect_mc.x = 516;
if (Game.playerStats[0] - Math.round(dmgDealt) <= 0)
{
// round dmg to 0 (were dead)
Game.playerStats[0] = 0;
}
else
{
// deal damage to us
Game.playerStats[0] -= Math.round(dmgDealt);
}
}
else if(attackEffect_mc.hitTestObject(wall))
{
//stop the player from moving (by doing nothing)
}
else
{
attackEffect_mc.x -= dmgSpeed;
}
}
else
{
// reset the position of the attack effect if we are not fighting
visible = false;
attackEffect_mc.x = 516;
}
}
}
}
}
This example of Event.ENTER_FRAME is one of the few that actually are this complicated, the other Event.ENTER_FRAMEs tend to simply update values (ex: update a text field to display correct number variables)
For example: creating multiple vars inside an enterframe could clog
the memory and cause lag.
I've avoided doing stuff like this, as not only what you've said, but in my opinion I think it doesn't make sense to continuously define it in a function, I update these values only.
Resources
http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-8000.html
max links allowed
I am making a TDS in Flash CS4 using AS3 but there seems to be a problem. It's hard to explain so I'm gonna link the flash file. Click this.
This is the first time uploading a file for sharing so for those who can't or are unable to download the file, this is what happens:
Player has mouse rotation that is, Player looks at where the mouse is. On Mouse down I've put the script for creating bullets. The bullets are being created alright. But when the bullets move that's when the problem arises. Say that at position and rotation X, I shot 5 bullets and they are moving in X direction. Now if I shoot a bullet in Y position and rotation, the bullet that was created there goes in Y direction but so do all the other bullets that were created in the X position and direction. They change their course.
Here is the code for the game.
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Mouse;
import flash.events.TimerEvent;
public class Main extends MovieClip {
var player : Player = new Player();
//Customizable Weapon Settings
var bulletNumber:Number;//number of bullets per shot
var bulletOffset:Number;//bigger number = less acurate
var bulletSpeed:Number;//pixels per frame
var bulletMaxAge:Number;//1000 = 1 second
var reloadSpeed:Number;//1000 = 1 second
var randomNum:Number;
public static var xSpeed:Number;
public static var ySpeed:Number;
var bulletAngle:Number;
var timer:Number=0;
var flag:Boolean;
//other variables (do not edit)
var mouseClicked:Boolean=false;
var radians:Number=Math.PI/180;
public function Main() {
player.x=stage.stageWidth/2;
player.y=stage.stageHeight/2;
stage.addChild(player);
player.gotoAndStop(5);
loadWeapon("Machine Gun");
addEventListener(Event.ENTER_FRAME,on_enter_frame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
}
public function onMouseDownHandler(event:MouseEvent) {
//trace("Mouse Down");
mouseClicked=true;
flag=true;
}
public function onMouseUpHandler(event:MouseEvent) {
//trace("Mouse Up");
mouseClicked=false;
flag=false;
timer=0;
}
public function loadWeapon(weaponType:String) {
switch (weaponType) {
case "Machine Gun" :
//bulletNumber = 100;
bulletOffset=10;
bulletSpeed=10;
bulletMaxAge=1000;
break;
}
}
function on_enter_frame(e:Event) {
trace("Click: "+ mouseClicked);
fireWeapon();
}
function fireWeapon() {
//check if mouse is clicked
//if true, create bullet
if (mouseClicked) {
createBullet();
player.gotoAndStop(10);
} else {
player.gotoAndStop(1);
}
}
public function createBullet() {
var bullet : Bullet2= new Bullet2();
bullet.x=player.x;
bullet.y=player.y;
if (flag) {
timer++;
if (timer==10) {
trace("lol");
//calculate random bullet offset.
randomNum = Math.random() * (bulletOffset);
//set bullet firing angle
bulletAngle = (player.rotation + randomNum) * radians;
//set bullet speed based on angle
xSpeed=Math.cos(bulletAngle)*bulletSpeed;
ySpeed=Math.sin(bulletAngle)*bulletSpeed;
//trace (bulletAngle);
stage.addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, runForest);
//mouseClicked = false;
timer=0;
}
}
function runForest(e:Event) {
bullet.x+=xSpeed;
bullet.y+=ySpeed;
}
}
}
}
Things that I've tried:
1) I put the "runForest()" funtion outside of "createbullet()" function which give me a "1120: Access of undefined property bullet." Error. (Which doesn't make sense since I am giving it a enter frame event listener.)
2) For solving this, I made the bullet variable global and declared it inside the "createbullet()" function like this- "var bullet : Bullet2;" And inside createbullet()- "bullet = new Bullet2();" That gives me a completely different output.
3) I put the "runForest()" function in its own class file. But the same thing is happening.
I was referring to a Tutorial that used AS2. This is the link.
Help me solve this please.
Thanks!
Review this code:
//set bullet speed based on angle
xSpeed=Math.cos(bulletAngle)*bulletSpeed;
ySpeed=Math.sin(bulletAngle)*bulletSpeed;
then take a look at how these variables for speed are created:
public static var xSpeed:Number;
public static var ySpeed:Number;
You have 1 variable for the x direction of the speed. If there is only one variable, there can only be 1 value for speed.
that's why all your bullets are moving in the same direction, because they all share that one single value for speed, which causes them to go into the same direction.
Your Main class is doing everything at the moment and you should really refactor some of that code into several other classes.
Even your own understanding of the code you are writing is not reflected by the code, your comment says:
//set bullet speed based on angle
Now why is that bullet speed a variable of Main? Object oriented programming is made exactly for that. You can literally turn your plain English description of the desired behaviour into code.
When you say that you "want to have Bullets", then create a Bullet class.
When you say "each Bullet object should have its own speed", then add a property to that class that is the speed.
You will encounter the same problem with your weapons and the same solution applies.
I have this baffling problem with Flash AS3 that I have been attempting to solve for a long time. I have a notion that perhaps this is a bug with the flash player, but perhaps you can shed some insight.
I have a MovieClip in Flash that is a star for 10 frames, a circle for another 10, and then a square for another 10, after which it will gotoAndPlay(1), replaying the animation. This MovieClip extends an AS3 class I have called FlipClip.
FlipClip has a function in it called reverseClip. This function's purpose is to flip certain graphic children around an axis every time Flash launches the EXIT_FRAME event.
public function FlipClip()
{
//as soon as this is instantiated, add the eventListener
addEventListener(Event.EXIT_FRAME,flipTheClip);
}
public function flipTheClip(e:Event)
{
trace("currentFrame = " + currentFrame);
//for sake of simplicity, we will flip every child
for (var i=0; i<numChildren; i++)
{
var targetClip = getChildAt(i);
var axis = 10;
//if the target child has not already been flipped...
if (Math.abs(targetClip.scaleX) / targetClip.scaleX != -1)
{
//reverse the child's direction with scaleX and move based on the axis
targetClip.scaleX *= -1;
var dist:Number = targetClip.x - axis;
targetClip.x = axis - dist;
}
}
}
The obvious outcome is that every time we exit a frame, all of the graphic elements are flipped horizontally around x=10, and every ten frames the shape of the MovieClip changes from a star, to a circle, to a square. Right?
Nope.
The MovieClip does successfully flip around that axis, but then a strange problem occurs. The animation stops. The MovieClip is stuck as an eternal star. And Flash doesn't even recognize that the animation has stopped, as we get this output over and over;
currentFrame = 1
currentFrame = 2
currentFrame = 3
currentFrame = 4
...
currentFrame = 30
currentFrame = 1
All the way up to 30, at which point it goes back to one. The clip is still playing, but somehow the graphic elements are not updating!
Is this a problem with the flash player? Is this a problem with the code? Any help is appreciated!
I've uploaded the files for the .fla and .as on dropbox. I'm still figuring out how to embed something like that, but for now I'm gonna hope this link works for you.
https://www.dropbox.com/sh/hcljutesblichpp/AABKQ4Kn8OTwfTaeh0I3nnOZa?dl=0
UPDATE:
If I convert every individual shape into a MovieClip within the parent MovieClip, it plays correctly. However, this is not very memory efficient or feasible with complex animations. Hopefully this bit of information can help you solve the problem.
There are couple of thing which you need to take care.
You don't need to flip element based on the numChildren as it always
return 1 as on each frame you will get single children.
you also don't need to do the check another condition
Math.abs(targetClip.scaleX) / targetClip.scaleX != -1 to set the
flip.
And also you need to use ENTER_FRAME instead of EXIT_FRAME.
ENTER_FRAME works for the current frame whereas EXIT_FRAME works for
previous frame.
Use the below code.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.setTimeout;
public class FlipClip extends MovieClip
{
var mInstance
var prevX;
public function FlipClip()
{
//as soon as this is instantiated, add the eventListener
addEventListener(Event.ENTER_FRAME,flipTheClip);
mInstance = this;
//mInstance.visible = false;
}
public function flipTheClip(e)
{
this.scaleX *= -1;
prevX = this.x;
if(this.scaleX < 0)
this.x = prevX + this.width
else
this.x = prevX - this.width
}
}
}
Paste above code in FlipClip.as file and change the frame rate to 1.
You need to update the moviClip placement based on your requirement.
Hope above answer solve your problem.
You need to remove listener for EXIT_FRAME before playing animation. Also you are Flipping your movieClip here but not adding any code for playing it.
Paste below code in your FlipClip.as file.
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.setTimeout;
public class FlipClip extends MovieClip
{
var mInstance
public function FlipClip()
{
//as soon as this is instantiated, add the eventListener
addEventListener(Event.EXIT_FRAME,flipTheClip);
mInstance = this;
mInstance.visible = false;
}
private function playallAnimation()
{
this.gotoAndPlay(1);
}
public function flipTheClip(e)
{
removeEventListener(Event.EXIT_FRAME,flipTheClip);
//for sake of simplicity, we will flip every child
for (var i=0; i<numChildren; i++)
{
var targetClip = getChildAt(i);
var axis = 10;
//if the target child has not already been flipped...
if (Math.abs(targetClip.scaleX) / targetClip.scaleX != -1)
{
//reverse the child's direction with scaleX and move based on the axis
targetClip.scaleX *= -1;
var dist:Number = targetClip.x - axis;
targetClip.x = axis - dist;
}
}
setTimeout(function()
{
mInstance.visible = true;
playallAnimation();
},200);
}
}
}
Hope this will work for you.
I've got 6 objects with drag'n'drop and hitTest function. It's obvious that when i drag one object to their hitTest area it's correct.
But my question is that i want to control these 6 object by mouse and keyboard keys. if i dont want to drag them, then i can move them by key arrows.
Second question is that when these two methods will be done if i want to move an object by key then i grab first free object which is not "hitTested"
Can I do these things in AS3? Pretty weird solutions in these project but i need to know if is possible.
Here's my code for drag and drop functions
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
public class Main extends MovieClip
{
var xPos:int;
var yPos:int;
var errors:int;
var wins:int;
public function Main():void
{
addListeners(objectone,objecttwo,objectthree,objectfour,objectfive);
errors = 1;
wins = 1;
}
private function getPosition(target:Object):void
{
xPos = target.x;
yPos = target.y;
}
private function dragObject(e:MouseEvent):void
{
info.text = "";
getPosition(e.target);
e.target.startDrag(true);
}
private function stopDragObject(e:MouseEvent):void
{
if (e.target.hitTestObject(getChildByName(e.target.name + "Target")))
{
e.target.x = getChildByName(e.target.name + "Target").x;
e.target.y = getChildByName(e.target.name + "Target").y;
info.text = "Bingo!";
wins++;
}
else
{
e.target.x = xPos;
e.target.y = yPos;
bledy.text = "Wrong: " + errors++;
}
if(errors == 4) {
errors = 1;
gotoAndPlay(2);
}
if(wins == 9) {
wins = 1;
gotoAndStop(3);
}
e.target.stopDrag();
}
private function addListeners(... objects):void
{
for (var i:int = 0; i < objects.length; i++)
{
objects[i].addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
objects[i].addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
}
}
}
}
Please give me some code tips!
Cheers!
For sure it is possible... and for sure there are several options to achieve that.
For the keyboard interaction I suggest you check FocusManager class so you can learn how to cycle trough selected objects using tab, and get the focused object.
Then listen for stage keyboard events to move with arrows the object.
Regarding dragging to move check the DragManager class related samples if using Flex, or look up google for tutorials in flash.
So I have been making this game with Action Script 3, and CS5.5. What you are trying to do is avoid asteroids while you fly through space. I thought it would be cool to have the planets in out solar system be moving down the screen throughout the game in the background. Kinda to make it look like you were flying pass them. The way I did this was I added five to their y coordinate each frame per second. Once their y coordinate reached 600 ( the bottom of the screen ) I would add a new planet which would do the same thing. For some reason once I got to Saturn everything got weird. Saturn came to early, and so did Uranus. I had no idea what was going on. I have been frustrated with this for a good hour. Here is the part where I think there is the problem.
public function onTick(timerEvent:TimerEvent):void
{
earth.PlanetMovement(5);
if (earth.y==600)
{
mars.PlanetsStart(300, -100);
addChild( mars );
levels=levels+5;
}
mars.PlanetMovement(5);
if (mars.y==600)
{
jupiter.PlanetsStart(300,-150);
addChild (jupiter);
levels=levels+10;
}
jupiter.PlanetMovement(5);
if (jupiter.y==600)
{
saturn.PlanetsStart(300,-155);
addChild (saturn);
levels=levels+20;
}
saturn.PlanetMovement(5);
if (saturn.y==600)
{
uranus.PlanetsStart(300,-160)
addChild ( uranus);
levels=levels+25;
}
uranus.PlanetMovement(5);
PlanetMovement, and PlanetsStart are two functions in the Planets class. If you need more info please tell me.
EDIT: I guess I should explain further. PlanetsStart is a function that has the starting coordinate of each movieclip. So once earth reached a y coordinate of 600, then mars starts at (300, -100). Then it is added to the screen. levels is a variable that raises the score each fps. PlanetMovement is how much each movieclip will move each fps. If I were to use >= then the score would raise too much.
This is exactly what happens. earth shows up where it is supposed to. Then mars showd up on time. Then for some reason Saturn pops up in the middle of mars, and Jupiter. After this Saturn reaches the bottom making Uranus appear. Then Jupiter reaches the bottom, and everything works as it should. Saturn shows up, and then Uranus in order
Ugh. Unfortunately, this is more complex than you are implying. It would really help you to write a code plan of some sort first, develop some psuedo-code that will show basic logic (and the logic errors), and then code after that.
Below is an example of a better way to structure this idea. However, I don't think your idea is terrible memory conscious. You should only bring in the planets as needed, and remove them as needed as well. Look into a technique called "object pooling" to help better structure this.
Solar system class:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class SolarSystem extends MovieClip
{
private var PLANET_NAMES:Array = new Array("earth", "mars", "jupiter", "saturn", "uranus");
// you will have to fix these values:
private var PLANET_STARTS:Array = new Array(new Point(300, -100), new Point(300, -100),new Point(300, -100),new Point(300, -100),new Point(300, -100));
private var planets:Array;
private var maxY:Number = 600;
private var speed:Number = 5;
private var levels:Number = 0;
private var levelIncrement:Number = 5;
public function SolarSystem()
{
super();
addEventListener(Event.ADDED_TO_STAGE, initHnd, false, 0, true);
}
private function initHnd(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initHnd);
runPlanets();
addEventListener(Event.ENTER_FRAME, frameHnd, false, 0, true);
}
private function runPlanets():void
{
for (var i:int = 0; i < PLANET_NAMES.length; i++)
{
planets[i] = new Planet();
planets[i].name = PLANET_NAMES[i];
Planet(planets[i]).startPlanet(PLANET_STARTS[i]);
this.addChild(planets[i]);
}
}
private function frameHnd(e:Event):void
{
if(planets && planets.length > 0){
// move all the planets until they are too low, then remove them.
// decrementing loop because planets will be being removed occasionally.
for (var i:int = planets.length -1; i >= 0; i--)
{
if (planets[i] && Planet(planets[i]).y >= maxY) {
// this seems very strange to me, but it will replicate what your code says:
levels += (levels + levelIncrement);
try {
removeChild(Planet(planets[i]));
planets[i] = null;
}catch (e:Error) { }
} else if ( Planet(planets[i]).isMoving ){
Planet(planets[i]).movePlanet(0, speed);
}
}
} else {
removeEventListener(Event.ENTER_FRAME, frameHnd);
}
}
}
}
and here is the planet class:
package
{
import flash.display.MovieClip;
public class Planet extends MovieClip
{
private var _isMoving:Boolean = false;
public function Planet()
{
super();
}
public function startPlanet(sx:Number, sy:Object):void
{
this.x = sx;
this.y = sy;
isMoving = true;
}
public function movePlanet(dx:Number, dy:Number):void
{
if (isMoving)
{
this.x += dx;
this.y += dy;
}
}
public function get isMoving():Boolean
{
return _isMoving;
}
public function set isMoving(value:Boolean):void
{
_isMoving = value;
}
}
}
Again, this is not the best way to do this, but it is more manageable to group concepts and actions into classes.
HTH.
Right off the bat I can see a major issue. Change all the statements from:
if (saturn.y==600)
to
if (saturn.y>=600)
Just saying == means that you're only going to trigger the conditional in the case that your Y value is exactly 600.
Note you can also change stuff like levels = levels+20 to simply levels += 20;.
When moving stuff around like this it is a good idea to check the position by using ">=" instead of "==". What if your object starts at y=1? Then it will never reach 600 it will just keep on going down forever.
Also you seem to be calling PlanentMovement even when PlanetsStart hasn't been called yet.