Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › ActionScript 3.0 Flash Help- Understanding this for loop...
New Posts  All Forums:Forum Nav:

ActionScript 3.0 Flash Help- Understanding this for loop...

post #1 of 4
Thread Starter 
Hi all,

Making a game at the moment and I've lifted some code from one of my teacher's examples. Thinking it would help. The code basically gets the mcstar graphic and repeats it all over the screen looping it for a continuous moving effect. The stars go from right to left. I wanted it to go from top to bottom. I thought it would be easy but it's completely confusing me. As whenever I mess around with the x and y's or - and +'s it'll work to a degree but the stars will run out and won't loop... My understanding of the loop is pretty limited especially the for loop so if someone could clear it up as well it'd be great...

Code:

public function moveBackground(){
for(var layer=0;layer<3;layer++){
for(var j = 1; j>-1;j--){
starFieldArray[layer][j].x-=backgroundSpeed*(layer+1);
if(starFieldArray[layer][j].x < -screenWidth){
removeChild(starFieldArray[layer][j]);
starFieldArray[layer][j] = null; 
starFieldArray[layer].splice(j,1);
starFieldInit(layer);
}
}
}
}//end of moveBackground


public function starFieldInit(layer){
//trace("Adding layer " + layer.toString());
starField = new MovieClip();
starField.y = 0;
if(starFieldArray[layer].length==0){
starField.x = 0;
}else{
starField.x = starFieldArray[layer][0].x + screenWidth;
}
for(var i=0;i<100;i++){
star = new mcStar();
star.width = 5;
star.height = 5;
star.x = Math.random()*screenWidth;
star.y = Math.random()*screenHeight;
star.gotoAndPlay((Math.round(Math.random()*140)));
starField.addChild(star);
}
addChildAt(starField,0);
starFieldArray[layer].push(starField);

}
That's all the code for the starfield and moving them...

Thanks!!
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #2 of 4
new to programming but by looking at it i would possibly recommend changing your declaration at the top. change x to y and change it to screen height from width
OverKill
(18 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X6 1055T Gigabyte 890FXA ATI FirePro V3800 ATI FirePro V3800 
RAMHard DriveHard DriveOptical Drive
16gb GSkill 10666 Seagate Western Digital  LG Blue Ray 
CoolingOSMonitorMonitor
stock air 7 Pro 64 bit (2)22" Acer x223w (2)23" Acer G235H 
KeyboardPowerCaseMouse
Microsoft Wireless Antec 900watt Antec 300 Wacom Tablet 
Audio
Logitech Speakers and sub 
  hide details  
Reply
OverKill
(18 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X6 1055T Gigabyte 890FXA ATI FirePro V3800 ATI FirePro V3800 
RAMHard DriveHard DriveOptical Drive
16gb GSkill 10666 Seagate Western Digital  LG Blue Ray 
CoolingOSMonitorMonitor
stock air 7 Pro 64 bit (2)22" Acer x223w (2)23" Acer G235H 
KeyboardPowerCaseMouse
Microsoft Wireless Antec 900watt Antec 300 Wacom Tablet 
Audio
Logitech Speakers and sub 
  hide details  
Reply
post #3 of 4
Thread Starter 
That was the first thing I tried... But unfortunately when I did this the stars would fail to loop

Edit:

Sorry meant to say that it worked. But the stars come from the bottom to top which is the opposite of what I wanted!

Code:
starFieldArray[layer][j].y-=backgroundSpeed*(layer+1);
When I change the - to a + right before =backgroundSpeed the stars come from the right direction but run out... Well fail to loop :S
Edited by kidshenlong - 4/15/11 at 3:53pm
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
post #4 of 4
Thread Starter 
bump.
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
Dark Star
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 - Currently Overclocking ;) Gigabyte GA-EP45-UD3P HD 6950 OCZ Blade LC DDR2 PC29200 4GB (GOT) 
Hard DriveOSMonitorKeyboard
640GB for Storage and OCZ Vertex 2e 60GB Windows 7 Ultimate-Signature Edition 3 x 22" -BenQ G2222HDL Logitech G15 
PowerCaseMouse
Corsair TX850 HAF 932 Logitech G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › ActionScript 3.0 Flash Help- Understanding this for loop...