|
-
Dynamically adding and removing multiple instances of same MCs after certain time.
Hey everyone,
After a little break in AS3 I'm back... and facing a problem. For a school project I'm trying to make a side scroller game in which the player automatically moves right and has to avoid branches.
Basically, depending on how well you do your speed (var) gets updated. After certain 'distance' (fake of course, as the player stays centered) I would like a n instance of Branch_MC to appear. Also, when that instance's x property reaches -20 I would like it removed.
I'd need approximately 135 branches so creating variables isn't really an option. I've been thinking about using arrays, but I'm unsure on how to use them to fit my needs.
As always, any and all help is much appreciated! =)
-Thanks in advance,
Patrick
-
rabid_Delineator
How are you handling your collision ? If you are centralizing everything in one enter frame event , i would just create a class , call it Branch , link it to your branch clip , or embed the asset , then instantiate new branches whenever you need them. You can add all of the branches to a sprite called , branchHolder. Then in you central enter frame event run a collision check , and also check the x position of each child of branchHolder. If that x is - 20 , then kill it and remove it from the branchHolder.
-
Hey Attackrabbit,
Thanks for your quick reply! =) I'm not really following you all the way though =/
i would just create a class , call it Branch , link it to your branch clip , or embed the asset , then instantiate new branches whenever you need them. You can add all of the branches to a sprite called , branchHolder.
I've never really worked with classes like this before, so I'd love a bit more in-depth explanation. I.e.: How would I instantiate new branches from the class whenever I need them? Also, I'm not sure if I understand your branchHolder Sprite idea.
Again, thanks for the help so far.
-Patrick
-
rabid_Delineator
you would create a new actioscript file in flash , or whatever IDE you are working in. something like this
Code:
package com.game.obstacles {
import flash.display.Sprite;
import flash.events.Event;
public class Branch extends Sprite {
public function Branch() {
addEventListener( Event.ADDED_TO_STAGE , init );
};
private function init( evt : Event ) : void {
removeEventListener( Event.ADDED_TO_STAGE , init );
};
};
};
Right so that does nothing as of now , but will allow you to create as many instance of that branch whenever and wherever you would like. then , assuming all your graphical assets are in the same .fla file , you would create a movieclip with your branch graphic in it. Then export that clip for actionscript , and point its class to , com.game.obstacles.Branch .
Then in your main class you say
Code:
import com.game.obstacles.Branch;
then in some function
Code:
var newBranch : Branch = new Branch();
addChild( branch );
and wallah. you can position that , etc . branch.x = 120 , etc.
what i was then recommending is that you put all your Branch instances into a single clip , so at the top of you main class
Code:
private var branchHolder : Sprite
;
then in your init or something somewhere
Code:
branchHolder = new Sprite();
addChild( branchHolder );
then in some func :
Code:
var newBranch : Branch = new Branch();
branchHolder.addChild( newBranch );
now you can trust that only objects of type Branch will be in branchHolder. So if you have a method specific to branch you can safely call it on any child object of branchHolder.
-
Hey, thanks again, but I think you misunderstood me (or I was just unclear).
Creating new Branches for every single of them seems like too much of a hassle, so I was looking for a way to generate Branches automatically and be able to control them using only one or two vars. Right now, this is my setup:
Code:
var branch_array:Array = new Array;
var distance:Number = 0; //On a keypress for the time being
var currentbranch:Number = 0; //Next in line to be added
stage.addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event):void{
for (var bi:int = 0; bi < 15; bi++) { //var bi = branch index
branch_array.push(new Branch_R());
}
switch(distance){
case 20:
branch_array[currentbranch].x = 0;
branch_array[currentbranch].y = 300;
addChild(branch_array[currentbranch]);
currentbranch += 1;
break;
case 60:
branch_array[currentbranch].x += 50;
branch_array[currentbranch].y = 300;
addChild(branch_array[currentbranch]);
currentbranch += 1;
break;
case 80:
branch_array[currentbranch].x += 100;
branch_array[currentbranch].y = 300;
addChild(branch_array[currentbranch]);
currentbranch += 1;
break;
case 100:
branch_array[currentbranch].x += 150;
branch_array[currentbranch].y = 300;
addChild(branch_array[currentbranch]);
currentbranch += 1;
break;
}
}
btn_d.addEventListener(MouseEvent.MOUSE_DOWN, incd);
function incd(MouseEvent):void{
distance += 20;
distancet.text = String(distance);
}
Now I'm still looking for a way to control everything from the entire array with one variable. Something along the lines of this (if it'd work):
Code:
branch_array[all].x += 10;
Sorry for all the trouble and thanks again for your help so far.
-Patrick
-
rabid_Delineator
if all you want to do is access everything in an array an do something to each item , use a for loop.
Code:
for( var i : uint = 0 ; i < branch_array.length ; i ++ ) {
var branchTarg : Branch = Branch( branch_array[ i ] );
branchTarg.x += 10;
};
No offense , but this is a terrible way to build a game. Your solution has many problems. For one , you create 15 branches all at once in a loop. You shouldn't need to do this , unless there are going to be 15 branches on screen at one time all coming at you , in which you might consider rethinking your game play design. You should only create what needs to be created , when it needs to be there. As soon as you can you should remove it. Ideally , you could probably reuse , the same 4 or five branches , and never remove them, just reset there positions to something new , and maybe change the height and width , or scale. For example , what if the user never makes it more then two seconds into the game , because he or she hit the first or second branch coming at them ? Well you have already created 15 branches in memory , 13 of them for no reason. Not to mention you are going to be running collision checks on things that arent even 'on screen' yet. This is just not good practice.
As for the class related bits , there's a couple of things to keep in my mind. I understand moving from as2 to as3 can be a bit cumbersome and you will want to fight it the whole way. I personally , came from a java , basic , c background, so all of these object oriented design models came naturally to me. I was super stoked to finally get away from as2. But you really really really need to leave that stuff behind. Otherwise there is no reason to use as3. You're not taking advantage of encapsulation at all , and the time and performance differences it makes. Anyway , just food for thought.
-
Thanks for all your input, it's much appreciated!
Performance is definitely something I need to keep in mind. (*boring background story at end of post).
I love the idea of only using 4 or 5 instances of Branch and simply repositioning/ resclaling them. This means I could just drop the whole array and simply create 5 vars.
However, I do have one last question if you don't mind.
Other instances of assets will be created throughout the game. Is there anyway I could use one name and use that to control every instance of that type of asset? Something like this:
Code:
var anyBranch:??? = any instance of the Branch class (vars branch_l, branch_r, branch_m, etc)
//In a loop function:
anyBranch.x -= 7;
if(anyBranch.x <= -20){
anyBranch.x = Math.random() * 50 + 600;
}
Is something along those lines possible, preventing me from checking each branch's .x position?
Of course this wouldn't nessecarily be practical for the branches, but it would be for general use 
*Background story time:
I've started a Communication & Multimedia Design education and this semester we're asked to create a flash game for a zoo which should run on one of their consoles (Win98 PCs with a trackball and mouse-button). This means performance is definitely an issue, so your tip could be a real lifesaver.
-Patrick
Last edited by senshi sentou; 12-15-2009 at 07:15 PM.
-
rabid_Delineator
yes to put it shortly , this is where that branchHolder clip would come into play. So every instance of Branch that you create, add it as a child to branchHolder. Again branchHolder is nothing more then a sprite you created , and added as a child to your application. then in your loop
Code:
for( var i : uint = 0 ; i < branchHolder.numChildren ; i ++ ) {
var anyBranch: MovieClip = MovieClip( branchHolder.getChildAt( i ) );
anyBranch.x -= 7;
if(anyBranch.x <= -20){
anyBranch.x = Math.random() * 50 + 600;
};
};
and thats it.
Now theres another way to do this , which i wouldnt recommend , but you can use array notation , or sometimes referred to as hash notation.
so again in the loop :
Code:
for( var i : uint = 0 ; i < totalBranches ; i ++ ) {
this["branch " + i ].x -= 7;
if(this["branch " + i ].x <= -20){
this["branch " + i ].x = Math.random() * 50 + 600;
};
};
now to use this you need to create another variable , called totalBranches and make sure you update it every time you add or remove a branch. Also when you create branches , you need to make sure you name them , "branch" + ( totalBranches + 1 ) , or in a loop , "branch" + i ;
-
Thank you so much for your time and patience; I owe you one! It worked like a charm! =)
-Patrick
Tags for this Thread
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
|