android - AS3 Game, Collecting points, multiple movie clips with the same instance name, not working -
i've decided create android touch screen game. complete , utter beginner , learning go.
my game has little elephant moves when press , hold on screen , falls when there no contact screen. aim collect many peanuts fly past possible gain highest score. pretty simple, you'd think so.
so far, i've managed point elephant can collide peanut , peanut disappears.
my issue right is, can't create more 1 peanut, same instance name of "peanut" because 1 work , others not recognized. i've done ole google search , nothing has given me right way go. give me clear answer of or go here?
if need more info, code or picture of i've got far understand let me know :)
- samantha
instance name must unique, , cannot use instance name find set of movie clips. should instead use array, , @ creating peanut add there using push()
, , @ collecting peanut, splice out.
in fact, whenever multi-instance class similar functionality (aka "collect"), use array
store references of these, know all of instances accessible through array.
a sample code:
var peanuts:array=new array(); function addpeanut(x:number,y:number):void { var peanut:peanut=new peanut(); // make peanut, somewhere peanut.x=x; peanut.y=y; peanuts.push(peanut); // array plays role game.addchild(peanut); // let displayed. "game" whatever container // have contain peanuts. } function removepeanut(i:int):void { // give index in array, it's better giving peanut var peanut:peanut=peanuts[i]; // array reference of peanut peanuts.splice(i,1); // remove reference array given index game.removechild(peanut); // , remove actual peanut display } function checkforpeanuts():void { // call every often, @ least once after peanuts , player move (var i:int=peanuts.length-1; i>=0; i--) { // going through peanuts in array var peanut:peanut=peanuts[i]; if (player.hittestobject(peanut)) { // of course, proper reference of "player"! // yay got 1 of peanuts! // scoring done // special effects, if removepeanut(i); // remove peanut } } }
Comments
Post a Comment