Note that there are some explanatory texts on larger screens.

plurals
  1. POAction Script 3. Spawn objects in different time
    primarykey
    data
    text
    <p>I'm creating game where falling 4 different objects from the sky and need to destroy them by clicking mouse button. My problem is that all 4 objects spawns at the same time. I need to make that randomly spawned all 4, but not in the same time and after player have 200 points I need to make that spawned and falled more objects. (I have working counter which counting points). And one more thing if you know how to make that objects not spawned on one other. I mean:</p> <p>Bad spawn.</p> <p><img src="https://i.stack.imgur.com/Q1tGS.png" alt="bad spawns"></p> <p>Here is my constant vars:</p> <pre><code>public static const GRAVITY:Number = 3; public static const HIT_TOLERANCE:Number = 50; //Powerup public static const APPLE_END_Y:Number = 640; public static const APPLE_SPAWN_CHANCE:Number = 0.02; //per frame per second public static const APPLE_START_Y:Number = 110; public static const APPLE_SPAWN_START_X:Number = 50; public static const APPLE_SPAWN_END_X:Number = 500; //Scoring public static const PLAYER_START_SCORE:Number = 0; public static const SCORE_PER_APPLE:Number = 10; </code></pre> <p>Here is part of my code:</p> <pre><code> public function startGame() { speed = C.PLAYER_SPEED; gravity = C.GRAVITY; score = C.PLAYER_START_SCORE; randomChance = C.APPLE_SPAWN_CHANCE; tol = C.HIT_TOLERANCE; apples = new Array(); mcGameStage.addEventListener(Event.ENTER_FRAME,update); } private function update(evt:Event) { //Spawn new apples if (Math.random() &lt; randomChance) { //spawn x coordinates var newPirmas = new Pirmas(); newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X; var newAntras = new Antras(); newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X; var newTrecias = new Trecias(); newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X; var newApple = new Apple(); newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X; //spawn y coordinates newPirmas.y = C.APPLE_START_Y; newAntras.y = C.APPLE_START_Y; newTrecias.y = C.APPLE_START_Y; newApple.y = C.APPLE_START_Y; apples.push(newPirmas); apples.push(newAntras); apples.push(newTrecias); apples.push(newApple); newPirmas.addEventListener(MouseEvent.CLICK, onClick); newAntras.addEventListener(MouseEvent.CLICK, onClick); newTrecias.addEventListener(MouseEvent.CLICK, onClick); newApple.addEventListener(MouseEvent.CLICK, onClick); mcGameStage.addChildAt(newPirmas,0); mcGameStage.addChildAt(newAntras,0); mcGameStage.addChildAt(newTrecias,0); mcGameStage.addChildAt(newApple,0); } //Move Apples for (var i = apples.length-1; i &gt;= 0; i--) { apples[i].y += gravity; if (apples[i].y &gt; C.APPLE_END_Y) { mcGameStage.removeChild(apples[i]); apples.splice(i,1); m_iLives--; if (!m_iLives) { trace("Game Over"); // newApple.removeEventListener(MouseEvent.CLICK, onClick); break; } } } txtScore.text = String(score); } function onClick(evt:MouseEvent):void{ var apples = evt.target; apples.visible = false; apples = tol; score += C.SCORE_PER_APPLE; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload