1

For school i am creating a basic game in AS3 for mobile, basically all you need to know is that I need a rectangle the width of the stage(380) (height doesn't matter) to spawn up the top then after a few seconds another spawns, and this process repeats infinitely until said otherwise. I haven't done much yet so i do not have much code to show but if anyone could tell me how it would be greatly appreciated.

I have the movement down packed, just not the spawning

var rectangle:Shape = new Shape;
var RecTimer:Timer = new Timer(10,10000);
RecTimer.addEventListener(TimerEvent.TIMER, fRecMovement);
RecTimer.start()


function fRecMovement (e:TimerEvent):void {

rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectangle.graphics.drawRect(0, 0, 480,45.49); // (x spacing, y spacing, width, height)
rectangle.graphics.endFill();
addChild(rectangle); // adds the rectangle to the stage
rectangle.y +=1


}

1 Answers1

0

You need to create a new shape each time you want to add something, using the new keyword. But you also need to keep track of each rectangle that you create so you can move it. In the code below, the rectangles array is a list of all the rectangles. Then you can go through the list and move each rectangle.

var RecTimer:Timer = new Timer(10,10000);
RecTimer.addEventListener(TimerEvent.TIMER, onTimer);
RecTimer.start();

var rectangles:Array = []; // a list of all the rectangles we've made so far

function spawnRectangle():void {
    var rectangle:Shape = new Shape();
    rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
    rectangle.graphics.drawRect(0, nextRectangleY, 480, 45.49); // (x spacing, y spacing, width, height)
    rectangle.graphics.endFill();
    addChild(rectangle); // adds the rectangle to the stage

    rectangles.push(rectangle); // adds the rectangle to our list of rectangles
}

function moveAllRectangles():void {
    for each (var rectangle:* in rectangles) {
        rectangle.y += 1;
    }
}

function onTimer(e:TimerEvent) {
    spawnRectangle();
    moveAllRectangles();
}

This will create a new rectangle every time the timer runs, but you probably want to do it slower than that. Maybe you could do that with a separate timer.

Also, if you keep letting this code run it's going to slow down a lot and use a lot of memory as it's going to keep creating new rectangles and never stop! See if you can figure out a way to limit that.

Jezzamon
  • 1,453
  • 1
  • 15
  • 27
  • Thanks heaps, at the moment it doesnt seem to move the rectangle ill take a look at but but thanks so much, this has saved me a lot of time! maybe an idea is to 'removeChild' each rectangle when it reaches a certain y value? – jackmrobertson Oct 23 '15 at 01:32
  • Success, i got it to move, tiny little error in the moving loop, next step is if it is possible for spacing between the spawning because as of this point in time it is a constant stream of rectangles, would you happen to have any ideas? – jackmrobertson Oct 23 '15 at 01:52
  • It happens because you're creating rectangles too fast and moving them too slowly (which is what happens in the code I posted), and so you won't be able to see any gap between them. So you need to either make rectangles slower or move them faster (probably a bit of both) – Jezzamon Oct 23 '15 at 01:52
  • I gotten it to work, now i have encountered a new problem(sorry for all these) but after a while the rectangles stop moving but they keep spawning, if it is something to do with the timer could you please help me. – jackmrobertson Oct 23 '15 at 03:51
  • @jackmrobertson - You don't post new questions in the comments of your old question. Make a new one with the new issue. – Jimbo Jonny Oct 23 '15 at 03:54