-
How to make resets with movie clip in AS3
I'm trying to convert an old AS2 game of mine into AS3. The old game is here:
http://www.flashninjaclan.com/zzz100_.php
Right now I'm having trouble with the background, I've duplicated the stars in the main time(note I am programming the game in the main timeline, no additional AS files).
Code:
import flash.display.*;
var bigstar:thestar;
var midstar:thestar;
var smallstar:thestar;
var stars: Array=new Array();
var i:int;
for(i=1;i<=25;i++)
{
bigstar=new thestar();
bigstar.width=18;
bigstar.height=13;
bigstar.x = Math.random() * stage.stageWidth;
bigstar.y = -30;
addChild(bigstar);
stars.push(bigstar);
}
But I can't figure out how to get them to fall and then reset to the top after they reach the bottom. The old AS2 code was just this:
Code:
onClipEvent(load)
{
this._y=random(400);
this._x=random(550);
this.speed=random(2)+1;
function reset()
{
this._x=random(550);
this._y=-30;
this.speed=random(2)+1;
}
}
onClipEvent(enterFrame)
{
this._y+=this.speed;
if(this._y>410)
{
this.reset();
}
}
That was the code in the clip, which AS3 does not allow.
-
add an enterFrame listener to the main game, and everytime it's called, it will do all the checking.
Code:
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(evt:Event):void
{
//loop through your stars and make them do their thing
for(var i:int = 0; i < stars.length; i++)
{
stars[i].y = stars[i].y+30;
if(stars[i].y >= 410)
{
stars[i].y = -30;
}
}
}
-
Code:
var bigstar:thestar;
var midstar:thestar;
var smallstar:thestar;
var stars:Array = [];
var i:int = 25;
while (i-- > 0) {
bigstar=new thestar();
bigstar.width = 18;
bigstar.height = 13;
starReset(bigstar);
addChild(bigstar);
stars.push(bigstar);
}
addEventListener(Event.ENTER_FRAME, doLoop);
function doLoop(e:Event) {
var star:thestar;
for each (star in stars) {
star.y += star.speed;
if (star.y > 410) {
starReset(star);
}
}
}
function starReset(mc) {
mc.x = Math.random() * stage.stageWidth;
mc.y = -30;
mc.speed = Math.random() * 2 + 1;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|