Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-09-2012, 05:40 AM   #1
nimach
Registered User
 
Join Date: Feb 2012
Posts: 30
Question Use each for loop code in arrays

hello.
I have a problem to use each code in for loop. I create 2 Circles in my stage with addChild code. And I gave them function ( mc.x += 2; ) and the time they get to the end of stage I gave the function ( mc.x -= 2; ) but my problem is when one of the circles get to end and wants to move to the right the other circle move with him to the right. and I think the each code in for loop helps me. but I don't Know how to use it in my actions.
my code is:
ActionScript Code:
var mc1:Circle = new Circle(); var mc2:Circle = new Circle(); var circles:Array = [mc1,mc2]; var maxX:Number = stage.stageWidth - 40; var minX:Number = 0; var right:Boolean = true; var left:Boolean = false; var speed:Number = 2; for (var i:uint = 0; i < 2; i++) {     addChild(circles[i]);     circles[i].x = Math.random() * stage.stageWidth;     circles[i].y = Math.random() * stage.stageHeight;     } addEventListener(Event.ENTER_FRAME , movement); function movement(e:Event):void {     //I want to use each code here...     for (var i:uint = 0; i < 2; i++)     {         if (circles[i].x >= maxX)         {             right = false;             left = true;         }         if (circles[i].x <= minX)         {             right = true;             left = false;         }         if (right)         {             circles[i].x +=  speed;         }         if (left)         {             circles[i].x -=  speed;         }     } }
What should I change in this code to use each for loop..?
nimach is offline   Reply With Quote
Old 02-09-2012, 07:57 AM   #2
GLY
Registered User
 
Join Date: Sep 2009
Location: china
Posts: 8
Send a message via ICQ to GLY Send a message via MSN to GLY Send a message via Skype™ to GLY
Default

Code:
var mc1:Circle = new Circle();
var mc2:Circle = new Circle();

var circles:Array = [mc1,mc2];

var maxX:Number = stage.stageWidth - 40;
var minX:Number = 0;
var right:Boolean = true;
var left:Boolean = false;
var speed:Number = 2;

for (var i:uint = 0; i < 2; i++)
{
    addChild(circles[i]);
    circles[i].x = Math.random() * stage.stageWidth;
    circles[i].y = Math.random() * stage.stageHeight;
    
}
addEventListener(Event.ENTER_FRAME , movement);

function movement(e:Event):void
{
    //I want to use each code here...
    for each(var item:Circle in circles)
    {
        if (item.x >= maxX)
        {
            right = false;
            left = true;
        }
        if (item.x <= minX)
        {
            right = true;
            left = false;
        }
        if (right)
        {
            item.x +=  speed;
        }
        if (left)
        {
            item.x -=  speed;
        }
    }
}

Last edited by GLY; 02-09-2012 at 08:32 AM. Reason: code
GLY is offline   Reply With Quote
Old 02-09-2012, 07:59 AM   #3
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

You have two circles, right? And you have two variables, right and left, to tell you whether the circles are moving right or left. The two variables that you have, though, essentially track just one property, and could have been done in just one variable.

So the problem is that you are tracking just one property (call it direction). But you are using that one property for both circles. Each circle should really have its own property, so that it "knows" what direction it's going.

Here's one way of doing it. In your initial for loop, give each circle a dynamic property called dir (for direction). The dir property can have a value of either 1 or -1. A value of 1 will cause it to move right, and a value of -1 will cause it to move left. Then, rather than use an if statement, all you have to do is multiply this dir property by the speed, and you will get either a positive or negative speed value as a result. When you add this value to the circle's x, if the value added is negative, it will move left. If it is positive, it will move right:
ActionScript Code:
var circles:Array = new Array(); var quantity:int = 2; var maxX:Number = stage.stageWidth - 40; var minX:Number = 0; var speed:Number = 2; for (var i:uint = 0; i < quantity; i++) {     circles[i] = new Circle();     addChild(circles[i]);     circles[i].x = Math.random() * stage.stageWidth;     circles[i].y = Math.random() * stage.stageHeight;     circles[i].dir = 1; //dynamic property, 1 = right, -1 = left } addEventListener(Event.ENTER_FRAME , movement); function movement(e:Event):void {     for (var i:uint = 0; i < circles.length; i++)     {         if (circles[i].x >= maxX)         {             circles[i].dir = -1;         }         if (circles[i].x <= minX)         {            circles[i].dir = 1;         }         circles[i].x += circles[i].dir * speed;     } }
I also made the above more dynamic, so that you can just change the quantity variable to a higher number to create more circles. There's no real reason for manually creating mc1, mc2, etc, and then manually putting them into an array like you were doing.
__________________
My Tutorials * My Website

Last edited by Mazoonist; 02-09-2012 at 08:04 AM.
Mazoonist is offline   Reply With Quote
Old 02-09-2012, 08:16 AM   #4
GLY
Registered User
 
Join Date: Sep 2009
Location: china
Posts: 8
Send a message via ICQ to GLY Send a message via MSN to GLY Send a message via Skype™ to GLY
Default

Code:
package {
	import flash.display.Sprite;
	
	public class Circle extends Sprite {
		public var right:Boolean = true;
		public var left:Boolean = false;
		function Circle() {
			graphics.beginFill(0xff0000, 1);
			graphics.drawCircle(0, 50, 50);
			graphics.endFill();
		}
	}
}

var mc1:Circle = new Circle();
var mc2:Circle = new Circle();

var circles:Array = [mc1,mc2];

var maxX:Number = stage.stageWidth - 40;
var minX:Number = 0;
var speed:Number = 2;

for (var i:uint = 0; i < 2; i++)
{
    addChild(circles[i]);
    circles[i].x = Math.random() * stage.stageWidth;
    circles[i].y = Math.random() * stage.stageHeight;
    
}
addEventListener(Event.ENTER_FRAME , movement);

function movement(e:Event):void
{
    //I want to use each code here...
    for (var i:uint = 0; i < 2; i++)
    {
        if (circles[i].x >= maxX)
        {
            circles[i].right = false;
            circles[i].left = true;
        }
        if (circles[i].x <= minX)
        {
            circles[i].right = true;
            circles[i].left = false;
        }
        if (circles[i].right)
        {
            circles[i].x +=  speed;
        }
        if (circles[i].left)
        {
            circles[i].x -=  speed;
        }
    }
}

Last edited by GLY; 02-09-2012 at 08:34 AM.
GLY is offline   Reply With Quote
Old 02-09-2012, 08:41 AM   #5
nimach
Registered User
 
Join Date: Feb 2012
Posts: 30
Default

thank you very much. It really Helped me.
but the code I write in this post is not my all codes. It's just an example of my problem. my real code is a lot of circles that move diagonal way (both right and up) but in this direction code you gave me don't have direction in Y position. if I want to use it in Y position, What code should I use?

Last edited by nimach; 02-09-2012 at 08:43 AM.
nimach is offline   Reply With Quote
Old 02-09-2012, 09:16 AM   #6
GLY
Registered User
 
Join Date: Sep 2009
Location: china
Posts: 8
Send a message via ICQ to GLY Send a message via MSN to GLY Send a message via Skype™ to GLY
Default

I'm sorry to tell you my English is poor. sometimes I can't understand your mean.I don't know do you wanna like this....
Code:
package {
	import flash.display.Sprite;
	
	public class Circle extends Sprite {
		public var nDirX:int = 1;
		public var nDirY:int = 1;
		function Circle() {
			graphics.beginFill(0xff0000, 1);
			graphics.drawCircle(0, 50, 50);
			graphics.endFill();
		}
	}
}


var mc1:Circle = new Circle();
var mc2:Circle = new Circle();

var circles:Array = [mc1,mc2];

var maxX:Number = stage.stageWidth - 40;
var minX:Number = 0;
var maxY:Number = stage.stageWidth - 40;
var minY:Number = 0;
var speed:Number = 2;

for (var i:uint = 0; i < 2; i++)
{
    addChild(circles[i]);
    circles[i].x = Math.random() * stage.stageWidth;
    circles[i].y = Math.random() * stage.stageHeight;
    
}
addEventListener(Event.ENTER_FRAME , movement);

function movement(e:Event):void
{
    for (var i:uint = 0; i < 2; i++)
    {
        if (circles[i].x >= maxX)
        {
            circles[i].nDirX = -1;
        }
        if (circles[i].x <= minX)
        {
           circles[i].nDirX = 1;
        }
        circles[i].x += circles[i].nDirX * speed;
		 if (circles[i].y >= maxY)
        {
            circles[i].nDirY = -1;
        }
        if (circles[i].y <= minY)
        {
           circles[i].nDirY = 1;
        }
        circles[i].y += circles[i].nDirY * speed;
    }
}
GLY is offline   Reply With Quote
Old 02-09-2012, 10:09 AM   #7
nimach
Registered User
 
Join Date: Feb 2012
Posts: 30
Default

I use your code you gave me but It's have errors in its way.
if you test your code, maybe you could fix it.
nimach is offline   Reply With Quote
Old 02-09-2012, 10:12 AM   #8
SanderSoulwax
Senior Member
 
Join Date: Sep 2010
Posts: 131
Default

It's not his code, his code is fine. It's the way you're using his code, for sure..
SanderSoulwax is offline   Reply With Quote
Old 02-09-2012, 10:17 AM   #9
nimach
Registered User
 
Join Date: Feb 2012
Posts: 30
Default

I test the code. Do you test it? If you think it works good please send me .fla and .swf of this code to see what's my way problems.
nimach is offline   Reply With Quote
Old 02-09-2012, 10:41 AM   #10
SanderSoulwax
Senior Member
 
Join Date: Sep 2010
Posts: 131
Default

I didn't test it, but I can see there's nothing wrong with it.

You've probably never worked with different classes, have you?

The code is actually two files.

Circle.as
ActionScript Code:
package {     import flash.display.Sprite;         public class Circle extends Sprite {         public var nDirX:int = 1;         public var nDirY:int = 1;         function Circle() {             graphics.beginFill(0xff0000, 1);             graphics.drawCircle(0, 50, 50);             graphics.endFill();         }     } }

Your main .as file (probably)
ActionScript Code:
var mc1:Circle = new Circle(); var mc2:Circle = new Circle(); var circles:Array = [mc1,mc2]; var maxX:Number = stage.stageWidth - 40; var minX:Number = 0; var maxY:Number = stage.stageWidth - 40; var minY:Number = 0; var speed:Number = 2; for (var i:uint = 0; i < 2; i++) {     addChild(circles[i]);     circles[i].x = Math.random() * stage.stageWidth;     circles[i].y = Math.random() * stage.stageHeight;     } addEventListener(Event.ENTER_FRAME , movement); function movement(e:Event):void {     for (var i:uint = 0; i < 2; i++)     {         if (circles[i].x >= maxX)         {             circles[i].nDirX = -1;         }         if (circles[i].x <= minX)         {            circles[i].nDirX = 1;         }         circles[i].x += circles[i].nDirX * speed;          if (circles[i].y >= maxY)         {             circles[i].nDirY = -1;         }         if (circles[i].y <= minY)         {            circles[i].nDirY = 1;         }         circles[i].y += circles[i].nDirY * speed;     } }

And another thing: if you're having errors in your project always tell us WHAT errors, otherwise we cannot really help you.
SanderSoulwax 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 10:40 AM.

///
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.