AS3 How do I make these objects loop endlessly?

Hayden

Here is my code so far, I am trying to create a top down car game and here is the code I have so far, I am currently trying to get the cars to loop but I am struggling to find a way to do it, please try and help me out with the code or point me in the right direction if you can please and thank you in advance

import flashx.textLayout.utils.CharacterUtil;

var result:Number = Math.random() * 100
var randomX:Number = Math.random() * stage.stageWidth
var background = new Background;
var char = new Char();
var car1 = new Car1();
var car2 = new Car2();
var car3 = new Car3();
var car4 = new Car4();


car1.x = Math.random()* stage.stageWidth;
car2.x = Math.random()* stage.stageWidth;
car3.x = Math.random()* stage.stageWidth;
car4.x = Math.random()* stage.stageWidth;
background.x = 200;
char.x = 700/2;

car1.y = -0;
car2.y = -150;
car3.y = -300;
car4.y = -450;
background.y = 200;
char.y = 450;

addChild(background);
addChild(char);
addChild(car1);
addChild(car2);
addChild(car3);
addChild(car4);

char.gotoAndStop("car");

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

function fl_EnterFrameHandler(event:Event):void
{
    car1.y +=12;
    car2.y +=12;
    car3.y +=12;
    car4.y +=12;
}


stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_PressKeyToMove);

function fl_PressKeyToMove(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
char.x += 15;
if (char.hitTestObject(barrier1))
{
char.x -=15 ;
}
break;
}
case Keyboard.LEFT:
{
char.x -= 15;
if (char.hitTestObject(barrier2))
{
char.x +=15 ;
}
break;
}
}
}
stage.addEventListener(Event.ENTER_FRAME, detectCollision);
function detectCollision(event:Event)  {

       if(char.hitTestObject(car1))
       {
       char.gotoAndStop("boom");
       }
       if(char.hitTestObject(car2))
       {
       char.gotoAndStop("boom");
       }
       if(char.hitTestObject(car3))
       {
       char.gotoAndStop("boom");
       }
       if(char.hitTestObject(car4))
       {
       char.gotoAndStop("boom");
       }

}
Brian

If you're trying to get cars to reposition to the top of the screen as @onekidney guessed, you could easily do so by using the % operator:

function fl_EnterFrameHandler(event:Event):void
{
    car1.y = (car1.y + 12) % stage.stageHeight;
    car2.y = (car2.y + 12) % stage.stageHeight;
    car3.y = (car3.y + 12) % stage.stageHeight;
    car4.y = (car4.y + 12) % stage.stageHeight;
}

You can read more about the modulo operator in the documentation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I stop it from looping endlessly

From Dev

How do I make a contextmanager with a loop inside?

From Dev

How do I make this loop work?

From Dev

AS3, How to make a loop instead of using all these IF-statement

From Dev

How do I make this loop once, break then continue the loop?

From Dev

How do I loop through objects and categorize by timestamps in Javascript?

From Dev

How do I dynamically create class objects in a loop?

From Dev

How do I make a list of list objects In VB.NET?

From Dev

How do I make objects disappear after another one touches it?

From Dev

How do I make a function to show/hide different objects?

From Dev

How do I make players on Roblox spawn as different objects?

From Dev

How do I make a list of objects parcelable in Android?

From Dev

How do I make a schema with objects in array? Mongoose

From Dev

How Do I Make an Array of Objects a Reactive Data Source?

From Dev

How do I make a heap queue of objects in python?

From Dev

How do I make my MergeSort generic for different objects?

From Dev

AS3: How do i draw shapes in my own class

From Dev

How do I produce a result in a string? (AS3)

From Dev

how do i add style to a textfield in AS3?

From Dev

AS3 - How do I code a particular frame?

From Dev

How do I iterate through this object properly AS3

From Dev

How do I automatically save my preferences on as3?

From Java

Javascript. How do you make a loop that creates objects, then those objects being pushed into an array?

From Dev

Why the objects I make in a loop become the same?

From Dev

How do I make HTTP requests inside a loop in NodeJS

From Dev

Java: How do I make a loop which creates arrays?

From Java

How do I make variable weights dynamic in lmer for loop

From Dev

How do I refactor a recursion occurring in a for loop to make it a tail call?

From Dev

How do I make this async foreach loop work with promises?

Related Related

  1. 1

    How do I stop it from looping endlessly

  2. 2

    How do I make a contextmanager with a loop inside?

  3. 3

    How do I make this loop work?

  4. 4

    AS3, How to make a loop instead of using all these IF-statement

  5. 5

    How do I make this loop once, break then continue the loop?

  6. 6

    How do I loop through objects and categorize by timestamps in Javascript?

  7. 7

    How do I dynamically create class objects in a loop?

  8. 8

    How do I make a list of list objects In VB.NET?

  9. 9

    How do I make objects disappear after another one touches it?

  10. 10

    How do I make a function to show/hide different objects?

  11. 11

    How do I make players on Roblox spawn as different objects?

  12. 12

    How do I make a list of objects parcelable in Android?

  13. 13

    How do I make a schema with objects in array? Mongoose

  14. 14

    How Do I Make an Array of Objects a Reactive Data Source?

  15. 15

    How do I make a heap queue of objects in python?

  16. 16

    How do I make my MergeSort generic for different objects?

  17. 17

    AS3: How do i draw shapes in my own class

  18. 18

    How do I produce a result in a string? (AS3)

  19. 19

    how do i add style to a textfield in AS3?

  20. 20

    AS3 - How do I code a particular frame?

  21. 21

    How do I iterate through this object properly AS3

  22. 22

    How do I automatically save my preferences on as3?

  23. 23

    Javascript. How do you make a loop that creates objects, then those objects being pushed into an array?

  24. 24

    Why the objects I make in a loop become the same?

  25. 25

    How do I make HTTP requests inside a loop in NodeJS

  26. 26

    Java: How do I make a loop which creates arrays?

  27. 27

    How do I make variable weights dynamic in lmer for loop

  28. 28

    How do I refactor a recursion occurring in a for loop to make it a tail call?

  29. 29

    How do I make this async foreach loop work with promises?

HotTag

Archive