Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-23-2010, 10:12 AM   #1
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default [AS3] MC walking speed error?

I created a game, in my game, we can create army with xspeed.

Every frame, the army x is plus with xspeed.

But i got an error.

the army speed plus more than its xspeed.

Example:
i got 1 army, the xspeed is 3
when i got 2 army, the xspeed become 6
when i got 3 army, the xspeed become 9.

So the army xspeed become faster and faster. how to fix this error?

This is my code:
Create the mc.
Code:
var army_arr:Array = [];

function call_army():void{
	var army:MovieClip = new Swordmc();
	army.xspeed = 3;
	addChildAt (army,1);
	army.x = 50 ;
	army.y = 350 ;
	army.addEventListener (Event.ENTER_FRAME, walk);
	army_arr.push (pasukan);
}
updating mc (walking)
Code:
function walk(event:Event ):void{
	for (var i:int = 0; i<army_arr.length;i++){
		var army:MovieClip = army_arr[i];
		army.x += army.xspeed;
	}
}
Sorry for my english.
teambuddies is offline   Reply With Quote
Old 12-24-2010, 08:49 AM   #2
chinchang
Indie Game Developer
 
Join Date: Apr 2009
Location: India
Posts: 33
Send a message via Skype™ to chinchang
Default

Hi,
There is no error in the speed code as such. What I found wrong is this :
ActionScript Code:
army_arr.push (pasukan);

It should be :
ActionScript Code:
army_arr.push (army);

Try correcting it.
chinchang is offline   Reply With Quote
Old 12-25-2010, 08:37 AM   #3
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default

Nope, it's still give error.
teambuddies is offline   Reply With Quote
Old 12-25-2010, 04:39 PM   #4
zyxstand
Platinum Member
 
Join Date: Dec 2010
Posts: 193
Default

it seems you're adding an ENTER_FRAME event listener for each army (so if you have 3 armies, you have 3 event listeners) and in each army eventhandler, your iterating through all the armies and changing there position there. instead, you should add the event listener once in your main code, not in the code where an army is pushed.

also, instead of your updating code:
Code:
	for (var i:int = 0; i<army_arr.length;i++){
		var army:MovieClip = army_arr[i];
		army.x += army.xspeed;
	}
try using this code:
Code:
	for each (var anarmy:MovieClip in army_arr)
		anarmy.x += anarmy.xspeed;
as you can see, the code is cleaner and MUCH more readable. also notice i have taken out the { and } since it's now only 1 line. i'm guessing you're gonna need to turn this back into a block statement, but just thought you should know that
Merry... holidays!
zyxstand is offline   Reply With Quote
Old 12-26-2010, 10:32 AM   #5
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default

Wow it's works! Thanks!

Oh yeah, i want to ask. how to add the event listener on stage?

For now i use timer. Thanks
teambuddies is offline   Reply With Quote
Old 12-26-2010, 03:00 PM   #6
zyxstand
Platinum Member
 
Join Date: Dec 2010
Posts: 193
Default

what most people do is this:
the stage variable only exists in your main .as file. what you should do is create a static public variable so that all other classes can access it. like this (where "Main.as" is your main class):
Code:
//in your Main.as file put this where you define variables:
static public var stg:Stage = stage;

// to access this from other classes use this:
Main.stg.addEventListener(event, call);
now every class can access the main stage. don't forget to import Stage to each package!
Code:
import flash.display.Stage;
zyxstand is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:28 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.