Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Use each for loop code in arrays
 User Name Remember Me? Password
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 Thread Tools Rate Thread Display Modes
 02-09-2012, 05:40 AM #1 nimach Registered User   Join Date: Feb 2012 Posts: 30 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..?
 02-09-2012, 07:57 AM #2 GLY Registered User   Join Date: Sep 2009 Location: china Posts: 8 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
 02-09-2012, 07:59 AM #3 Mazoonist Site Contributor     Join Date: Jun 2006 Posts: 3,160 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.
 02-09-2012, 08:16 AM #4 GLY Registered User   Join Date: Sep 2009 Location: china Posts: 8 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.
 02-09-2012, 08:41 AM #5 nimach Registered User   Join Date: Feb 2012 Posts: 30 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.
 02-09-2012, 09:16 AM #6 GLY Registered User   Join Date: Sep 2009 Location: china Posts: 8 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; } }```
 02-09-2012, 10:09 AM #7 nimach Registered User   Join Date: Feb 2012 Posts: 30 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.
 02-09-2012, 10:12 AM #8 SanderSoulwax Senior Member   Join Date: Sep 2010 Posts: 131 It's not his code, his code is fine. It's the way you're using his code, for sure..
 02-09-2012, 10:17 AM #9 nimach Registered User   Join Date: Feb 2012 Posts: 30 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.
 02-09-2012, 10:41 AM #10 SanderSoulwax Senior Member   Join Date: Sep 2010 Posts: 131 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.

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

All times are GMT. The time now is 03:30 AM.

///

## Categories

 Contact Us - Home Page - Archive - Privacy Statement - Top

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.