arrays - Collectibles with different score values -


i have array of randomly spawning collectibles. when collide player, removed stage , score increases scorevalue. want each collectible in array have different scorevalue, haven't been able it. below of recent code pertaining this.

public class main_enemyspawntest_3 extends movieclip {     // variables     var player:player = new player;     var collectables:array = [collectable1, collectable2, collectable3];     var collectablesrandomnumber:int = math.random() * (collectables.length);     var collectable = new collectables[collectablesrandomnumber];     var score:uint = 0;      // constructor function     public function main_enemyspawntest_3():void     {         // listeners         addeventlistener(event.enter_frame, checkeveryframe);         spawncollectablesbutton.addeventlistener(mouseevent.click, spawncollectablesclick);     }      // enter frame, check every frame     public function checkeveryframe(event:event):void     {         // score         messagedisplay.text = string (score);     }      // hit test     function hittest (event:event):void     {         // hit test collectables         if (collectable.hittestobject (player) == true)         {             var scorevalue:uint;              if (stage.contains (collectable))             {                 if (collectables[0])                 {                     scorevalue = 100;                 }                 else if (collectables[1])                 {                     scorevalue = 300;                 }                 else if (collectables[2])                 {                     scorevalue = 700;                 }                 removechild (collectable);                 score = score + scorevalue;             }         }     } } 

at moment, collectables being spawned button click. code below, if helps.

// collectable spawn button function spawncollectablesclick (event:mouseevent):void {     var oldcollectable = collectable;     if (stage.contains (oldcollectable))     {         removechild (oldcollectable);     }        var collectablepositionrandomnumber:int = math.random() * 3;      var collectablepositionx:int = stage.width - (collectable.width * -0.5);     collectable = new collectables[math.floor(math.random() * collectables.length)];     collectable.x = collectablepositionx;     globalspeed = 5;     if (collectablepositionrandomnumber == 0)     {         collectable.y = topposition;     }     else if (collectablepositionrandomnumber == 1)     {         collectable.y = centerposition;     }     else if (collectablepositionrandomnumber == 2)     {         collectable.y = bottomposition;     }     addchild (collectable);     trace (collectable); } 

the problem coming if (collectables[0]): return true long collectables[0] isn't false/null/0/nan or undefined. you're going adding 100 , else ifs skipped.

you can give collectable own score value when creating in spawncollectablesclick:

collectable.scorevalue = ... 

then use increment score

function hittest (event:event):void {     // hit test collectables     if (collectable.hittestobject (player) == true)     {         score = score + collectable.scorevalue;         removechild (collectable);     } } 

as side note, if collectible1, collectible2, , collectible3 different classes, you're defeating purpose of classes. if 3 things have same behavior different values things, can instances of same class different member variable values.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -