-
ActionScript: Duplicating Objects in a Loop?
So I posted a question before and thanks to the community I was able to overcome an issue I was struggling with and made great progress in my program. Unfortunately I have run into another problem.
The follow code produces a strawberry image that falls from the top of the screen to the bottom. I tried using a for loop to create 10 different instances of this but it doesn't seem to work for me. Anyone have any suggestions?
(Note: ranx is a number produced randomly earlier in the program)
Code:
var nstrawberry:strawberry = new strawberry();
stage.addChild(nstrawberry);
nstrawberry.x = ranx;
nstrawberry.y = stage.stageHeight-800;
nstrawberry.addEventListener(Event.ENTER_FRAME, do_move_fruit);
function do_move_fruit(f:Event):void
{ //start do_move_fruit
f.currentTarget.y+=15;
if (f.currentTarget.y > 450)
{ //start if
f.currentTarget.removeEventList
ener(Event.ENTER_FRAME, do_move_fruit);
stage.removeChild(strawberry(f.currentTarget));
}//end if
}//end do_move_fruit
My second question is how do I check the collisions between the object the code above produces and the object the follow code produces:
Code:
function make_balloon(event:KeyboardEvent):void
{ //start make_balloon
switch (event.keyCode)
{ //start switch
case Keyboard.SPACE :
var nballoon:bballoon = new bballoon();
stage.addChild(nballoon);
nballoon.x = tobpos;
nballoon.y = stage.stageHeight / 2;
nballoon.addEventListener(Event.ENTER_FRAME, do_move);
break;
default :
break;
}//end switch
}//end make_balloon
function do_move(e:Event):void
{ //start do_move
e.currentTarget.y-=10;
if (e.currentTarget.y < 0)
{ //start if
e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_move);
stage.removeChild(bballoon(e.currentTarget));
} //end if
} //end do_move
I feel like the solution is much more simpler than I think it is but I just can't seem to figure it out.
Please help!
-
Code:
import flash.events.Event;
import flash.display.Sprite;
import flash.utils.Timer;
stop();
var tobpos:int;
var falling:Sprite;
var rising:Sprite;
var dropTimer:Timer;
var dropInterval:uint = 500;
var dropAmount:uint = 5;
var fallSpeed:uint = 5;
initGame();
function initGame():void {
tobpos = toby.x;
rising = new Sprite();
addChild(rising);
falling = new Sprite();
addChild(falling);
dropTimer = new Timer(dropInterval,dropAmount);
dropTimer.start();
dropTimer.addEventListener(TimerEvent.TIMER, make_berry);
stage.addEventListener(KeyboardEvent.KEY_DOWN, make_balloon);
stage.addEventListener(KeyboardEvent.KEY_DOWN, toby_Moving);
stage.addEventListener(Event.ENTER_FRAME, chk_hits);
}
function chk_hits(e:Event):void {
for (var i:uint=0; i<rising.numChildren; i++) {
for (var j:uint=0; j<falling.numChildren; j++) {
if (rising.getChildAt(i).hitTestObject(falling.getChildAt(j))) {
falling.getChildAt(j).removeEventListener(Event.ENTER_FRAME, do_falling);
falling.removeChild(falling.getChildAt(j));
rising.getChildAt(i).removeEventListener(Event.ENTER_FRAME, do_rising);
rising.removeChild(rising.getChildAt(i));
trace("hit one");
break;
}
}
}
}
function toby_Moving(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT :
toby.x -= 5;
tobpos = toby.x;
break;
case Keyboard.RIGHT :
toby.x += 5;
tobpos = toby.x;
break;
default :
break;
}
}
function make_berry(e:Event=null) {
var ranx:int = Math.round(Math.random() * stage.stageWidth);
var nstrawberry:strawberry = new strawberry();
falling.addChild(nstrawberry);
nstrawberry.x = ranx;
nstrawberry.y = stage.stageHeight - 800;
nstrawberry.addEventListener(Event.ENTER_FRAME, do_falling);
}
function do_falling(e:Event):void {
e.currentTarget.y += fallSpeed;
if (e.currentTarget.y > 450) {
e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_falling);
e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
}
}
function make_balloon(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.SPACE :
var nballoon:bballoon = new bballoon();
rising.addChild(nballoon);
nballoon.x = tobpos;
nballoon.y = stage.stageHeight / 2;
nballoon.addEventListener(Event.ENTER_FRAME, do_rising);
break;
default :
break;
}
}
function do_rising(e:Event):void {
e.currentTarget.y -= 10;
if (e.currentTarget.y < 0) {
e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_rising);
e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
}
}
-
Originally Posted by dawsonk
Code:
import flash.events.Event;
import flash.display.Sprite;
import flash.utils.Timer;
stop();
var tobpos:int;
var falling:Sprite;
var rising:Sprite;
var dropTimer:Timer;
var dropInterval:uint = 500;
var dropAmount:uint = 5;
var fallSpeed:uint = 5;
initGame();
function initGame():void {
tobpos = toby.x;
rising = new Sprite();
addChild(rising);
falling = new Sprite();
addChild(falling);
dropTimer = new Timer(dropInterval,dropAmount);
dropTimer.start();
dropTimer.addEventListener(TimerEvent.TIMER, make_berry);
stage.addEventListener(KeyboardEvent.KEY_DOWN, make_balloon);
stage.addEventListener(KeyboardEvent.KEY_DOWN, toby_Moving);
stage.addEventListener(Event.ENTER_FRAME, chk_hits);
}
function chk_hits(e:Event):void {
for (var i:uint=0; i<rising.numChildren; i++) {
for (var j:uint=0; j<falling.numChildren; j++) {
if (rising.getChildAt(i).hitTestObject(falling.getChildAt(j))) {
falling.getChildAt(j).removeEventListener(Event.ENTER_FRAME, do_falling);
falling.removeChild(falling.getChildAt(j));
rising.getChildAt(i).removeEventListener(Event.ENTER_FRAME, do_rising);
rising.removeChild(rising.getChildAt(i));
trace("hit one");
break;
}
}
}
}
function toby_Moving(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT :
toby.x -= 5;
tobpos = toby.x;
break;
case Keyboard.RIGHT :
toby.x += 5;
tobpos = toby.x;
break;
default :
break;
}
}
function make_berry(e:Event=null) {
var ranx:int = Math.round(Math.random() * stage.stageWidth);
var nstrawberry:strawberry = new strawberry();
falling.addChild(nstrawberry);
nstrawberry.x = ranx;
nstrawberry.y = stage.stageHeight - 800;
nstrawberry.addEventListener(Event.ENTER_FRAME, do_falling);
}
function do_falling(e:Event):void {
e.currentTarget.y += fallSpeed;
if (e.currentTarget.y > 450) {
e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_falling);
e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
}
}
function make_balloon(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.SPACE :
var nballoon:bballoon = new bballoon();
rising.addChild(nballoon);
nballoon.x = tobpos;
nballoon.y = stage.stageHeight / 2;
nballoon.addEventListener(Event.ENTER_FRAME, do_rising);
break;
default :
break;
}
}
function do_rising(e:Event):void {
e.currentTarget.y -= 10;
if (e.currentTarget.y < 0) {
e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_rising);
e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
}
}
Oh man that's awesome! Thank you! It's so neat too...I'd hate for you to see what mess I came up with
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
|