-
Random Object Remove
Hi guys, I'm trying to create a game where cubes fall from the sky. The cubes are randomly picked from an array and when you click on them they should go away. But for every cube you click it should add 2 new once. Here is what I have so far but its just not working
package {
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.*;
public class fallingcubes extends Sprite {
var cubes_missed:Number = 0
var count:Number = 0
var max_cubes = 10;
var cubes_on_stage = 0;
var gravity = 0.3;
var influence = 625;
var real_influence = Math.sqrt(influence);
var friction = 0.9;
var divider = 50;
var randomNum:int = 0
var randomA:int
var Absprite: Ab = new Ab(); //this is just 4 cube pictures/objects
var Agsprite: Ag = new Ag();
var Arsprite: Ar = new Ar();
var Aysprite: Ay = new Ay();
var AArray:Array = [Absprite, Agsprite, Arsprite, Aysprite];
public function fallingcubes() {
addEventListener(Event.ENTER_FRAME,main_enterframe );
}
public function main_enterframe(event:Event) {
randomA = Math.random()* ( 4 - 0) + 0;
trace (randomA)
if (cubes_on_stage<max_cubes) {
//adding a star
cubes_on_stage++;
var Absprite: Ab = new Ab();
var Agsprite: Ag = new Ag();
var Arsprite: Ar = new Ar();
var Aysprite: Ay = new Ay();
AArray[randomA].xspeed = 0;
AArray[randomA].yspeed = 0;
AArray[randomA].x = Math.random()*900+25;
AArray[randomA].y = 100
AArray[randomA].addEventListener(Event.ENTER_FRAME,cubesprite_ent erframe);
addChild(AArray[randomA]);
AArray[randomA].addEventListener(MouseEvent.CLICK,remove_cube)
function remove_cube (e:MouseEvent):void{
count += 1
trace("you hit me ", count)
removeChild(AArray[randomA])
max_cubes += 1
cubes_on_stage--
if (count == 20){
gravity = 0
stage.removeEventListener(MouseEvent.CLICK, remove_cube)
}
}
}
}
public function cubesprite_enterframe(event:Event) {
var current_star = (event.currentTarget as AArray[randomA])
current_star.yspeed += gravity;
current_star.xspeed *= friction;
current_star.yspeed *= friction;
current_star.y += current_star.yspeed;
current_star.x += current_star.xspeed;
current_star.rotation -= (current_star.xspeed+current_star.yspeed);
if (current_star.y>640) {
if (this.contains(current_star)){
current_star.removeEventListener(Event.ENTER_FRAME ,cubesprite_enterframe);
removeChild(current_star);
cubes_missed += 1
cubes_on_stage--;
trace ("You missed ", cubes_missed)
}
}
}
}
}
can anyone help
-
Prid - Outing
Hi,
we would appreciate it if you could upload the actual file instead of just posting the huge code here It's difficult to understand and debug the code without seeing your FLA file.
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
The File
Hi, thank you for your quick reply. The file was to large to upload but I've changed the png's to jpeg. Here it is!
Falling Cubes.zip
-
Maybe you can adapt this to what you are trying to do...
Code:
package {
import flash.display.*;
import flash.ui.*;
import flash.events.*;
import flash.utils.*;
public class FallingCubes extends Sprite {
var cubes_missed:int = 0;
var hit_count:int = 0;
var max_cubes = 10;
var gravity = 0.3;
var friction = 0.9;
var Absprite: Ab = new Ab();//this is just 4 cube pictures/objects
var Agsprite: Ag = new Ag();
var Arsprite: Ar = new Ar();
var Aysprite: Ay = new Ay();
var AArray:Array = [Ab,Ag,Ar,Ay];
var cubeArray:Array;
public function FallingCubes() {
addEventListener(Event.ADDED_TO_STAGE,init );
}
public function init(event:Event) {
removeEventListener(Event.ADDED_TO_STAGE,init );
cubeArray = new Array();
addCube();
stage.addEventListener(MouseEvent.CLICK, remove_cube);
addEventListener(Event.ENTER_FRAME, doLoop, false, 0, true);
}
private function addCube():void {
while (cubeArray.length < max_cubes) {
var randomNum:int = Math.random()* ( 4 - 0) + 0;
var cube = new AArray[randomNum]();
cube.x = Math.random() * 900 + 25;
cube.y = 100;
cube.xspeed = 1;
cube.yspeed = 1;
addChild(cube);
cubeArray.push(cube);
}
}
private function remove_cube(event:MouseEvent):void {
var className:Class = getDefinitionByName(getQualifiedClassName(event.target)) as Class;
if (AArray.indexOf(className) > -1) {
var cube:* = event.target as className;
cube.parent.removeChild(cube);
cubeArray.splice(cubeArray.indexOf(event.target), 1);
hit_count++;
max_cubes++;
trace("You hit ", hit_count);
if (hit_count == 20) {
// add whatever...
gravity = 0;
} else {
addCube();
}
}
}
private function doLoop(event:Event) {
for (var i = cubeArray.length; i > 0; i--) {
var cube:* = cubeArray[i - 1];
cube.yspeed += gravity;
cube.xspeed *= friction;
cube.yspeed *= friction;
cube.y += cube.yspeed;
cube.x += cube.xspeed;
cube.rotation -= (cube.xspeed + cube.yspeed);
if (cube.y > 640) {
cube.parent.removeChild(cube);
cubeArray.splice(cubeArray.indexOf(cube), 1);
cubes_missed++;
trace("You missed ", cubes_missed);
// adds a new cube to replace one that moved off the bottom
addCube();
break;
}
}
}
}
}
-
Ah, thank you! I will give it a go when I get in and let you know what the outcome is.
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
|