User interaction with Leapmotion AS3 library - actionscript-3

I can connect the device and attach a custom cursor to one finger, but I can´t use any of the gestures to over/click a button or drag a sprite around, etc.
I´m using Starling in the project. To run this sample just create a Main.as, setup it with Starling and call this class.
My basic code:
package
{
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.events.LeapEvent;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.InteractionBox;
import com.leapmotion.leap.Pointable;
import com.leapmotion.leap.ScreenTapGesture;
import com.leapmotion.leap.Vector3;
import starling.display.Shape;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.TouchEvent;
/**
* ...
* #author miau
*/
public class LeapController extends Sprite
{
private var _controller:Controller;
private var _cursor:Shape;
private var _screenTap:ScreenTapGesture;
private var _displayWidth:uint = 800;
private var _displayHeight:uint = 600;
public function LeapController()
{
addEventListener(Event.ADDED_TO_STAGE, _startController);
}
private function _startController(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, _startController);
//adding controller
_controller = new Controller();
_controller.addEventListener( LeapEvent.LEAPMOTION_INIT, onInit );
_controller.addEventListener( LeapEvent.LEAPMOTION_CONNECTED, onConnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_DISCONNECTED, onDisconnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_EXIT, onExit );
_controller.addEventListener( LeapEvent.LEAPMOTION_FRAME, onFrame );
//add test button
_testButton.x = stage.stageWidth / 2 - _testButton.width / 2;
_testButton.y = stage.stageHeight / 2 - _testButton.height / 2;
addChild(_testButton);
_testButton.touchable = true;
_testButton.addEventListener(TouchEvent.TOUCH, doSomething);
//draw ellipse as a cursor
_cursor = new Shape();
_cursor.graphics.lineStyle(6, 0xFFE24F);
_cursor.graphics.drawEllipse(0, 0, 80, 80);
addChild(_cursor);
}
private function onFrame(e:LeapEvent):void
{
trace("ON FRAME STARTED");
var frame:Frame = e.frame;
var interactionBox:InteractionBox = frame.interactionBox;
// Get the first hand
if(frame.hands.length > 0){
var hand:Hand = frame.hands[0];
var numpointables:int = e.frame.pointables.length;
var pointablesArray:Array = new Array();
if(frame.pointables.length > 0 && frame.pointables.length < 2){
//trace("number of pointables: "+frame.pointables[0]);
for(var j:int = 0; j < frame.pointables.length; j++){
//var pointer:DisplayObject = pointablesArray[j];
if(j < numpointables){
var pointable:Pointable = frame.pointables[j];
var normal:Vector3 = pointable.tipPosition;
var normalized:Vector3 = interactionBox.normalizePoint(normal);
//pointable.isFinger = true;
_cursor.x = normalized.x * _displayWidth;
_cursor.y = _displayHeight - (normalized.y * _displayHeight);
_cursor.visible = true;
}else if (j == 0) {
_cursor.visible = false;
}
}
}
}
}
private function onExit(e:LeapEvent):void
{
trace("ON EXIT STARTED");
}
private function onDisconnect(e:LeapEvent):void
{
trace("ON DISCONNECT STARTED");
}
private function onConnect(e:LeapEvent):void
{
trace("ON CONNECT STARTED");
_controller.enableGesture( Gesture.TYPE_SWIPE );
_controller.enableGesture( Gesture.TYPE_CIRCLE );
_controller.enableGesture( Gesture.TYPE_SCREEN_TAP );
_controller.enableGesture( Gesture.TYPE_KEY_TAP );
}
private function onInit(e:LeapEvent):void
{
trace("ON INIT STARTED");
}
private function doSomething(e:TouchEvent):void
{
trace("I WAS TOUCHED!!!");
}
}
}
If a good code Samaritan can update this code to perform a screen tap gesture (or any interacion with any object), I will really appreciate this a lot.
Regards!

controller.enableGesture(Gesture.TYPE_SWIPE);
controller.enableGesture(Gesture.TYPE_SCREEN_TAP);
if(controller.config().setFloat("Gesture.Swipe.MinLength", 200.0) && controller.config().setFloat("Gesture.Swipe.MinVelocity", 500)) controller.config().save();
if(controller.config().setFloat("Gesture.ScreenTap.MinForwardVelocity", 30.0) && controller.config().setFloat("Gesture.ScreenTap.HistorySeconds", .5) && controller.config().setFloat("Gesture.ScreenTap.MinDistance", 1.0)) controller.config().save();
//etc...
Then catch it in the frame event listener:
private function onFrame( event:LeapEvent ):void
{
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
for ( var i:int = 0; i < gestures.length; i++ )
{
var gesture:Gesture = gestures[ i ];
switch ( gesture.type )
{
case Gesture.TYPE_SCREEN_TAP:
var screentap:ScreenTapGesture = ScreenTapGesture ( gesture);
trace ("ScreenTapGesture-> x: " + Math.round(screentap.position.x ) + ", y: "+ Math.round( screentap.position.y));
break;
case Gesture.TYPE_SWIPE:
var screenSwipe:SwipeGesture = SwipeGesture(gesture);
if(gesture.state == Gesture.STATE_START) {
//
}
else if(gesture.state == Gesture.STATE_STOP) {
//
trace("SwipeGesture-> direction: "+screenSwipe.direction + ", duration: " + screenSwipe.duration);
}
break;
default:
trace( "Unknown gesture type." )
}
}
}
When the event occurs, check the coordinates translated to the stage/screen and whether a hit test returns true.
EDIT: Considering I have no idea how to reliable get the touch point x/y (or better: how to translate them to the correct screen coordinates), I would probably do something like this in my onFrame event:
private function onFrame(event:LeapEvent):void {
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
var posX:Number;
var posY:Number;
var s:Shape;
if(frame.pointables.length > 0) {
var currentVector:Vector3 = screen.intersectPointable(frame.pointables[0], true); //get normalized vector
posX = 1920 * currentVector.x - stage.x; //NOTE: I hardcoded the screen res value, you can get it like var w:int = leap.locatedScreens()[0].widthPixels();
posY = 1080 * ( 1 - currentVector.y ) - stage.y; //NOTE: I hardcoded the screen res value, you can get it like var h:int = leap.locatedScreens()[0].heightPixels();
}
for(var i:int = 0; i < gestures.length; i++) {
var gesture:Gesture = gestures[i];
if(gesture.type == Gesture.TYPE_SCREEN_TAP) {
if(posX >= _button1.x &&
posX <= _button1.x + _button1.width &&
posY >= _button1.y &&
posY <= _button1.y + _button1.height) {
s = new Shape();
s.graphics.beginFill(0x00FF00);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Lisa tocada!");
}
else {
s = new Shape();
s.graphics.beginFill(0xFF0000);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Fallaste! Intentalo otra vez, tiempo: "+new Date().getTime());
}
}
}
}

Related

Is anyone interested in looking at my code for a game? I'm getting strange errors

I'm trying to make my first real, simple shooter game. I've read through tutorials online and instead of directly copying code, I tried to take things the next level to actually understand the code and add my own twists. With this in mind, I've recently been learning arrays and not sure if I am using them correctly.
I'm getting strange, intermittent
Error #2025 DisplayObject must be a child of the caller
messages in the output window, not the compiler window.
So, I don't know what line of code is generating this problem. By commenting out blocks of code I have narrowed it down to the modules labeled "CLEANUP MISSED ENEMIES" and "BULLET RATE OF SHOOTING", but the 'why' is beyond my understanding.
I am sure there will be many great comments pointing out conventions I get wrong and errors I am making. I value every chance to learn so please give your input as you see fit! I bet there are way better methods to do the things I am doing!
package{
import flash.events.Event;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.media.SoundChannel;
import com.greensock.*;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.ColorTransform;
import fl.motion.Color;
import flash.display.DisplayObject;
public class chopper extends MovieClip{
public function chopper(){
boot();
}
public function boot():void{
/////////VARS
var coptr:MovieClip = new copter();
var bulit:MovieClip = new bullit();
var mouseIsDn = false;
var speed = 0;
var PEW = false
var meter:MovieClip = new meters();
var bltArray:Array = new Array();
var airArray:Array = new Array();
var gndArray:Array = new Array();
var gameTIMERa:Timer = new Timer(5000);
var gameTIMERb:Timer = new Timer(10000);
stage.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
stage.addEventListener(MouseEvent.MOUSE_UP, unclicked);
function clicked(e:Event):void{
mouseIsDn = true;
}
function unclicked(e:Event):void{
mouseIsDn = false;
}
/////////INTRO SCREEN
var titl:MovieClip = new title();
addChild(titl);
var strt:MovieClip = new start();
addChild(strt);
var govr:MovieClip = new gOVER();
var ctINTRO:Color = new Color();
ctINTRO.setTint(Math.random()*0xFFFFFF, 0.5);
BG.transform.colorTransform = ctINTRO;
/////////MUSIC
var ChanAB:SoundChannel = new SoundChannel();
var Amusic:Sound = new musicA();
var Bmusic:Sound = new musicB();
ChanAB = Amusic.play(0, 9999);
/////////SFx
var ChanSFx:SoundChannel = new SoundChannel();
var Ashoot:Sound = new shootA();
var SFx:Array = new Array();
var Asuck:Sound = new suckA();
SFx.push(Asuck);
var Bsuck:Sound = new suckB();
SFx.push(Bsuck);
var Csuck:Sound = new suckC();
SFx.push(Csuck);
var gOver:Sound = new over();
/////////START
strt.startBTN.addEventListener(MouseEvent.CLICK, str);
function str(e:Event):void{
removeChild(titl);
removeChild(strt);
ChanAB.stop();
BG.transform.colorTransform = new ColorTransform;
////////////
ChanAB = Bmusic.play(0, 9999);
addChild(coptr);
TweenLite.to(coptr, 3, {x:157, y:316});
addChild(meter);
meter.x = 861;
meter.y = 9;
TweenLite.to(meter, 1, {x:735});
meter.life.gotoAndPlay(2);
gameTIMERa.addEventListener(TimerEvent.TIMER, addAIR);
gameTIMERa.start();
gameTIMERb.addEventListener(TimerEvent.TIMER, addGND);
gameTIMERb.start();
}
/////////ADDING ENEMIES
function addAIR(e:TimerEvent):void{
var Aair:MovieClip = new airA();
Aair.x = 805;
Aair.scaleX = .25
Aair.y = Math.random() * stage.stageHeight - Aair.height;
Aair.scaleY = .25
airArray.push(Aair);
addChild(Aair);
}
function addGND(e:TimerEvent):void{
var Agnd:MovieClip = new gndA();
Agnd.x = 805;
Agnd.scaleX = .25
Agnd.y = 430 + Math.floor(Math.random() * 36);
Agnd.scaleY = .25
gndArray.push(Agnd);
addChild(Agnd);
}
addEventListener(Event.ENTER_FRAME, startLoop);
function startLoop(e:Event):void{
if(coptr.x == 157 && coptr.y == 316){
speed = 10;
meter.life.gotoAndStop(10);
removeEventListener(Event.ENTER_FRAME, startLoop);
addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, shoot);
}
}
/////////COPTER SHOOTING
function shoot(e:Event):void{
if(PEW == false){
ChanSFx = Ashoot.play();
bulit.x = coptr.x + 5;
bulit.y = coptr.y;
bltArray.push(bulit);
addChild(bulit);
PEW = true;
}
}
/////////LIFE METER
function lifeMeterA(e:Event = null):void{
if(meter.life.width > 59){
meter.life.scaleX -= .04;
}
else if(meter.life.width < 59 && meter.life.width > 29){
meter.life.gotoAndStop(11);
meter.life.scaleX -= .04;
}
else if(meter.life.width < 29 && meter.life.width > 15){
meter.life.gotoAndStop(12)
meter.life.scaleX -= .04;
}
else if(meter.life.width < 15 && meter.life.width > 1.5){
meter.life.gotoAndPlay(2);
meter.life.scaleX -= .04;
}
}
function lifeMeterB(e:Event = null):void{
if(meter.life.width > 59){
meter.life.scaleX -= .01;
}
else if(meter.life.width < 59 && meter.life.width > 29){
meter.life.gotoAndStop(11);
meter.life.scaleX -= .01;
}
else if(meter.life.width < 29 && meter.life.width > 15){
meter.life.gotoAndStop(12)
meter.life.scaleX -= .01;
}
else if(meter.life.width < 15 && meter.life.width > 1.5){
meter.life.gotoAndPlay(2);
meter.life.scaleX -= .01;
}
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////MAIN LOOP
function gameLoop(e:Event) {
////////////////////////COPTER MOVEMENT
trace(bltArray);
trace(airArray);
trace(gndArray);
coptr.y += speed;
if(mouseIsDn){
if(speed > -5){
speed -= 1;
}
}else{
if(speed < 10){
speed += .25;
}
}
////////////////////////BULLET MOVEMENT
if(bulit){
bulit.x += 10;
}
if(coptr.y > stage.stageHeight - coptr.height*.5){
coptr.y = stage.stageHeight - coptr.height*.5;
}
else if(coptr.y < 0 + coptr.height*.5){
coptr.y = 0 + coptr.height*.5;
}
////////////////////////COLLISIONS
for(var i = 0; i<numChildren; i++){
if(getChildAt(i) is airA){
var b = getChildAt(i) as airA;
if(b.hitTestObject(coptr)){
lifeMeterA();
}
if(b.hitTestObject(bulit)){
airArray.shift();
removeChild(b);
removeChild(bltArray[0]);
PEW = false;
var m:uint = uint(Math.random() * 3);
ChanSFx = SFx[m].play();
}
}
else if(getChildAt(i) is gndA){
var c = getChildAt(i) as gndA;
if(c.hitTestObject(coptr)){
lifeMeterB();
}
if(c.hitTestObject(bulit)){
gndArray.shift();
removeChild(c);
removeChild(bltArray[0]);
PEW = false;
var n:uint = uint(Math.random() * 3);
ChanSFx = SFx[n].play();
}
}
}
if(coptr.y > stage.stageHeight - coptr.height){
lifeMeterA();
}
////////////////////////CLEANUP MISSED ENEMIES
if(airArray[0] && airArray[0].x < 0){
airArray.shift();
}
if(gndArray[0] && gndArray[0].x < 0){
gndArray.shift();
}
////////////////////////BULLET RATE OF SHOOTING
if(bltArray[0] && bltArray[0].x > stage.stageWidth){
PEW = false;
bltArray.shift();
}
////////////////////////END GAME
if(meter.life.width < 1.5){
removeEventListener(Event.ENTER_FRAME, gameLoop);
gameTIMERa.stop();
gameTIMERb.stop();
meter.life.gotoAndStop(13);
TweenLite.to(meter, .5, {x:861});
gameO();
}
}
function gameO(e:Event = null):void{
addChild(govr);
govr.x = 0;
govr.y = 0;
ChanAB.stop();
ChanSFx = gOver.play();
ChanSFx.addEventListener(Event.SOUND_COMPLETE, restart);
}
function restart(e:Event):void{
removeChild(govr);
removeChild(coptr);
boot();
}
}
}
}
Thanks in advance!
It means you're using removeChild() somewhere on an object whose parent is not the container you are trying to remove it from.
For example, this code would cause that error:
var shape:Shape = new Shape();
stage.removeChild(shape);
It's likely that you are calling removeChild() more than once somewhere in your code.

AS3 Frogger Pixel Perfect Collision

I have created a simple AS3 frogger game and used .hitTestObject to test if the frog hits any of the obstacles. This is not effective as the frog hits objects that aren't touching it at all. I am new to AS3 and have no idea where to start with coding for this. Any help would be appreciated!
package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.ui.*;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.Timer;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Main extends MovieClip
{
//set variables
public var frog_spriteInstance:frog_sprite;
public var safeZoneInstance1 : safeZone;
public var safeZoneInstance2: safeZone;
public var safeZoneInstance3 : safeZone;
public var whitecar : car1;
public var whitecar1 : car1;
public var whitecar2 : car1;
public var truck1 : truck_sprite;
public var truck2: truck_sprite;
public var redcar : car_sprite;
public var redcar2 : car_sprite;
public var redcar3 : car_sprite;
public var log1 : log_sprite;
public var log2 : log_sprite;
public var log3 : log_sprite;
public var letters : Letters;
public var letters2 : Letters_2;
public var letters3 : Letters_3;
public var letters4 : Letters_4;
public var health : HealthBar;
public var firstletter : firstLetter;
public var secondletter : secondLetter;
public var thirdletter : thirdLetter;
public var fourthletter : fourthLetter;
var mysound:Sound = new (mySound);
public function Main()
{
// constructor code
safeZoneInstance1 = new safeZone();
stage.addChild(safeZoneInstance1);
safeZoneInstance1.x = 300;
safeZoneInstance1.y=545
safeZoneInstance2 = new safeZone();
stage.addChild(safeZoneInstance2);
safeZoneInstance2.x = 300;
safeZoneInstance2.y=300
safeZoneInstance3 = new safeZone();
stage.addChild(safeZoneInstance3);
safeZoneInstance3.x = 300;
safeZoneInstance3.y=100
whitecar = new car1();
stage.addChild(whitecar);
whitecar.x = 300;
whitecar.y = 500
whitecar1 = new car1();
stage.addChild(whitecar1);
whitecar1.x = 550;
whitecar1.y = 480
whitecar2 = new car1();
stage.addChild(whitecar2);
whitecar2.x = 50;
whitecar2.y = 480
truck1 = new truck_sprite();
stage.addChild (truck1);
truck1.x = 1000;
truck1.y = 430
truck2 = new truck_sprite();
stage.addChild (truck2);
truck2.x = 300;
truck2.y = 430
redcar = new car_sprite();
stage.addChild (redcar);
redcar.x = 300;
redcar.y = 340
redcar2 = new car_sprite();
stage.addChild (redcar2);
redcar2.x = 100;
redcar2.y = 375
redcar3 = new car_sprite();
stage.addChild (redcar3);
redcar3.x = 500;
redcar3.y = 375
log1 = new log_sprite();
stage.addChild(log1);
log1.x = 300;
log1.y = 230
log2 = new log_sprite();
stage.addChild(log2);
log2.x = 100;
log2.y = 150
log3 = new log_sprite();
stage.addChild(log3);
log3.x = 500;
log3.y = 150
letters = new Letters();
letters.x = randomRange(100,500) ;
letters.y = randomRange(100,500);
stage.addChild(letters);
letters2 = new Letters_2();
letters2.x = randomRange(100,500) ;
letters2.y = randomRange(100,500);
stage.addChild(letters2);
letters3 = new Letters_3();
letters3.x = randomRange(100,500) ;
letters3.y = randomRange(100,500);
stage.addChild(letters3);
letters4 = new Letters_4();
letters4.x = randomRange(100,500) ;
letters4.y = randomRange(100,500);
stage.addChild(letters4);
frog_spriteInstance = new frog_sprite();
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
health = new HealthBar();
stage.addChild(health);
health.x = 130;
health.y = 20;
health.width = 100;
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveFrog);
stage.addEventListener(Event.ENTER_FRAME, movecars);
stage.addEventListener(Event.ENTER_FRAME, movetrucks);
stage.addEventListener(Event.ENTER_FRAME, moveredcars);
stage.addEventListener(Event.ENTER_FRAME, movelogs);
stage.addEventListener(Event.ENTER_FRAME,checkforcollision);
mysound.play();
}
public function moveFrog(e:KeyboardEvent)
{
// get the key pressed and then move the player
{
if (e.keyCode == Keyboard.UP)
{
frog_spriteInstance.rotation = 0;
frog_spriteInstance.y -= 50;
}
if (e.keyCode == Keyboard.DOWN)
{
frog_spriteInstance.rotation = 180;
frog_spriteInstance.y += 50;
}
if (e.keyCode == Keyboard.LEFT)
{
frog_spriteInstance.rotation = -90;
frog_spriteInstance.x -= 50;
}
if (e.keyCode == Keyboard.RIGHT)
{
frog_spriteInstance.rotation = 90;
frog_spriteInstance.x += 50;
}
}
}
function movecars(event:Event)
{
//move cars across screen and when they disappear, reappear at the other side
whitecar.x -= 3;
if (whitecar.x<-100){
whitecar.x=650;
}
whitecar1.x -= 3;
if (whitecar1.x<-100){
whitecar1.x=650;
}
whitecar2.x -= 3;
if (whitecar2.x<-100){
whitecar2.x=650;
}
}
function movelogs(event:Event)
{
//move logs across screen and when they disappear, reappear at the other side
log1.x -= 5;
if (log1.x<-100){
log1.x=650;
}
log2.x -= 5;
if (log2.x<-100){
log2.x=650;
}
log3.x -= 5;
if (log3.x<-100){
log3.x=650;
}
}
function movetrucks(event:Event)
{
//move trucks across screen and when they disappear, reappear at the other side
truck1.x +=3;
if (truck1.x>650){
truck1.x=-100;
}
truck2.x +=3;
if (truck2.x>650){
truck2.x=-100;
}
}
function moveredcars(event:Event)
{
//move red cars across screen and when they disappear, reappear at the other side
redcar.x +=3;
if (redcar.x>650){
redcar.x=-100;
}
redcar2.x +=3;
if (redcar2.x>650){
redcar2.x=-100;
}
redcar3.x +=3;
if (redcar3.x>650){
redcar3.x=-100;
}
}
function checkforcollision(event:Event)
{
//check for collisions
if (frog_spriteInstance.hitTestObject(log1) || (frog_spriteInstance.hitTestObject(log2) || (frog_spriteInstance.hitTestObject(log3) || (frog_spriteInstance.hitTestObject(whitecar) || (frog_spriteInstance.hitTestObject(whitecar1) || (frog_spriteInstance.hitTestObject(whitecar2) || (frog_spriteInstance.hitTestObject(log2) || (frog_spriteInstance.hitTestObject(truck1) || (frog_spriteInstance.hitTestObject(truck2) || (frog_spriteInstance.hitTestObject(redcar) || (frog_spriteInstance.hitTestObject(redcar2) || (frog_spriteInstance.hitTestObject(redcar3))))))))))))){
//reset frog if hits an obstacle
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
//reduce health bar
health.width -= 10;
}
//remove event listeners when health is empty
if (health.width == 0){
stage.removeEventListener(Event.ENTER_FRAME, movecars);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, moveFrog);
stage.removeEventListener(Event.ENTER_FRAME, movetrucks);
stage.removeEventListener(Event.ENTER_FRAME, moveredcars);
stage.removeEventListener(Event.ENTER_FRAME, movelogs);
}
//add letters to bottom of screen when hit correctly
if (frog_spriteInstance.hitTestObject(letters)){
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
stage.removeChild(letters);
firstletter = new firstLetter();
stage.addChild(firstletter);
firstletter.x=345;
firstletter.y=600;
}
if (frog_spriteInstance.hitTestObject(letters2)){
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
stage.removeChild(letters2);
secondletter = new secondLetter();
stage.addChild(secondletter);
secondletter.x=206;
secondletter.y=600;
}
if (frog_spriteInstance.hitTestObject(letters3)){
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
stage.removeChild(letters3);
thirdletter = new thirdLetter();
stage.addChild(thirdletter);
thirdletter.x=273;
thirdletter.y=600;
}
if (frog_spriteInstance.hitTestObject(letters4)){
stage.addChild(frog_spriteInstance);
frog_spriteInstance.x=300;
frog_spriteInstance.y=550;
stage.removeChild(letters4);
health.width -= 10;
fourthletter = new fourthLetter();
stage.addChild(fourthletter);
fourthletter.x=25;
fourthletter.y=620;
}
}
function randomRange(minNum:Number, maxNum:Number):Number
{
//random generator for letter positioning
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
}
}
Also my code is pretty dodgy so any suggestions on improvement are very welcome.
To perform pixel perfect hit test from two arbitrary display objects you have to draw them to bitmaps and use BitmapData/hitTest().
Here is a generalized function that does this:
function hitTestShapes(object1:DisplayObject, object2:DisplayObject, threshold:uint = 1):Boolean {
var bounds1:Rectangle = object1.getBounds(object1.parent);
var matrix1:Matrix = object1.transform.matrix;
matrix1.tx = object1.x - bounds1.x;
matrix1.ty = object1.y - bounds1.y;
var bmp1:BitmapData = new BitmapData(bounds1.width, bounds1.height, true, 0);
bmp1.draw(object1, matrix1);
var bounds2:Rectangle = object2.getBounds(object2.parent);
var matrix2:Matrix = object2.transform.matrix;
matrix2.tx = object2.x - bounds2.x;
matrix2.ty = object2.y - bounds2.y;
var bmp2:BitmapData = new BitmapData(bounds2.width, bounds2.height, true, 0);
bmp2.draw(object2, matrix2);
return bmp1.hitTest(bounds1.topLeft, threshold, bmp2, bounds2.topLeft, threshold);
}
Note, though, that drawing to bitmaps is rather slow, so be careful not to over-use this kind of function or you will have performance issues. In fact, it would be a good idea to use hitTestObject() first, which is very fast and detects bounding box intersections, then only call hitTestShapes() to refine your check. In other words:
if (frog_spriteInstance.hitTestObject(log1) && hitTestShapes(frog_spriteInstance, log1)) { }
As for suggestions on your code, there's a lot of potential for de-duplicating by way of arrays, loops, and functions. For example, instead of defining all your objects as separate variables, just store them all in an array (or vector):
public var safeZones:Array = [];
public var obstacles:Array = [];
public var letters:Array = [];
To populate the arrays, you can push values into them and use a function to consolidate all your duplicate object setup code:
(Code convention note: use "UpperCamelCase" for class names.)
public function Main(){
addSafeZone(300, 545);
addSafeZone(300, 300);
addSafeZone(300, 100);
addObstacle(WhiteCar, 300, 500);
addObstacle(WhiteCar, 550, 480);
addObstacle(WhiteCar, 50, 480);
addObstacle(Truck, 1000, 430);
addObstacle(Truck, 300, 430);
addObstacle(RedCar, 300, 340);
addObstacle(RedCar, 100, 375);
addObstacle(RedCar, 500, 375);
addObstacle(Log, 300, 230);
addObstacle(Log, 100, 150);
addObstacle(Log, 500, 150);
addRandomLetters(Letters_1);
addRandomLetters(Letters_2);
addRandomLetters(Letters_3);
addRandomLetters(Letters_4);
}
private function addSafeZone(x:Number, y:Number):void {
var safeZone:SafeZone = new SafeZone();
stage.addChild(safeZone);
safeZone.x = x;
safeZone.y = y
safeZones.push(safeZone);
}
private function addObstacle(spriteClass:Class, x:Number, y:Number):void {
var obstacle:Sprite = new spriteClass();
stage.addChild(obstacle);
obstacle.x = x;
obstacle.y = y
obstacles.push(obstacle);
}
private function addRandomLetters(lettersClass:Class):void {
var lettersSprite:Sprite = new lettersClass();
lettersSprite.x = randomRange(100, 500);
lettersSprite.y = randomRange(100, 500);
stage.addChild(lettersSprite);
letters.push(lettersSprite);
}
Now you can loop over the arrays to perform all your actions. For example, to check for a hit against any obstacle:
for each(var obstacle:DisplayObject in obstacles){
if(frog.hitTestObject(obstacle) && hitTestShapes(frog, obstacle)){
// The frog hit an obstacle!
}
}
You could also combine all our "move" functions into one, which moves each obstacle based on their type:
private function moveObstacles(e:Event):void {
for each(var obstacle in obstacles){
if(obstacle is WhiteCar){
obstacle.x -= 3;
}
else if(obstacle is RedCar){
obstacle.x += 3;
}
else if(obstacle is Truck){
obstacle.x += 3;
}
else if(obstacle is Log){
obstacle.x -= 5;
}
if(obstacle.x < -100){
obstacle.x = 650;
}
else if(obstacle.x > 650){
obstacle.x = -100;
}
}
}
Lastly, you really should only need a single ENTER_FRAME handler. Just call whatever functions you want from in there. Adding multiple ENTER_FRAME handlers can get troublesome to manage. For example, just do this:
addEventListener(Event.ENTER_FRAME, update);
private function update(e:Event):void {
moveObstacles();
doOtherStuff();
anythingYouNeedToDo();
}
This way you only need to removeEventListener(Event.ENTER_FRAME, update) to stop the game, not a whole bunch of ENTER_FRAME handlers.
I haven't tested any of this code, but you get the general idea. Let me know if you have any issues with it and I can help.

How to create a hierarchy Menu/Icon

I dont know if the question represent exactly what i want to ask, but i could`t figure how else to name what i want to do.
So im trying to create a 3x3 grid and in every cell i have a image.
I can click on any of the 2nd cell of a column only if the 1st of the same column has been clicked before that.What i mean is
i can click cell No:5 only if 4 has been clicked before that
i can click cell No:9 only if 8 and 7 has been clicked before that
i can click cell No:1,4,7 anytime.
and also when they are clicked their alpha gets to 0.1 (so i know that the cell has been clicked.)
currently i the logic for creating the grid and changing the alpha of any object i click but i don`t have the logic for the hierarchy.
public function Main()
{
var index:int = 0
var col:int = 3
var row:int = 3
for (var i:int = 0; i < row; i++)
{
for (var j:int = 0; j < col; j++)
{
var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1) ));
myImage = new Bitmap( new cls() );
var myImage_mc:MovieClip = new MovieClip();
myImage_mc.addChild(myImage)
myImage_mc.x = 100 + i * (myImage_mc.width + 10)
myImage_mc.y = 100 + j * (myImage_mc.height + 10)
this.addChild(myImage_mc);
myImage_mc.mouseEnabled = true
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
index++
}
}
}
private function onClick(ev:MouseEvent):void
{
trace(ev.target.name)
ev.currentTarget.alpha = 0.1;
}
Another way is to use a link variable to link element.
public class BitmapButton extends Sprite {
public var next:BitmapButton = null;
}
public class Main extends Sprite {
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var index:int = 0
var col:int = 3
var row:int = 3
for (var j:int = 0; j < col; j++)
{
var lastRowElement:BitmapButton = null;
for (var i:int = 0; i < row; i++)
{
var bmpd:BitmapData = new BitmapData( 100, 100, false, Math.floor( 0xff + Math.random() * 0xffffff ) );
var myImage:Bitmap = new Bitmap( bmpd );
var myImage_mc:BitmapButton = new BitmapButton();
myImage_mc.addChild(myImage)
myImage_mc.x = 100 + j * (myImage_mc.width + 10);
myImage_mc.y = 100 + i * (myImage_mc.height + 10);
myImage_mc.name = i + "_" + j;
this.addChild(myImage_mc);
myImage_mc.mouseChildren = false;
myImage_mc.mouseEnabled = false;
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
if ( lastRowElement == null ) {
myImage_mc.mouseEnabled = true;
} else {
lastRowElement.next = myImage_mc;
}
lastRowElement = myImage_mc;
index++
}
}
}
private function onClick(ev:MouseEvent):void
{
trace(ev.target.name)
if ( ev.target is BitmapButton ) {
var btn:BitmapButton = ev.target as BitmapButton;
ev.currentTarget.alpha = 0.1;
if( btn.next != null ) {
btn.next.mouseEnabled = true;
}
}
}
}
Something like this
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.utils.Dictionary;
public class Grid extends Sprite
{
private var _box : Dictionary = new Dictionary;
private var _conditions : Dictionary = new Dictionary;
private var _clicked : Dictionary = new Dictionary;
public function Grid()
{
var index:int = 0
var col:int = 3
var row:int = 3
for (var i:int = 0; i < row; i++)
{
for (var j:int = 0; j < col; j++)
{
//var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1) ));
//myImage = new Bitmap( new cls() );
var myImage_mc:MovieClip = new MovieClip();
//myImage_mc.addChild(myImage)
_box[ index ] = myImage_mc;
// Conditions for the 2nd and 3nd line etc.
if( i != 0 )
_conditions[ myImage_mc ] = _box[ index - col ];
// ------ DEBUG
myImage_mc.graphics.beginFill( 0xFFFFFF * Math.random() );
myImage_mc.graphics.drawRect(0,0,100,100);
myImage_mc.graphics.endFill();
// ------ END DEBUG
// Note i / j are invert from your example
myImage_mc.x = 100 + j * (myImage_mc.width + 10);
myImage_mc.y = 100 + i * (myImage_mc.height + 10);
this.addChild(myImage_mc);
myImage_mc.mouseEnabled = true
myImage_mc.addEventListener(MouseEvent.CLICK, onClick);
index++
}
}
}
protected function onClick(ev:MouseEvent):void
{
var neededClickElement : MovieClip = _conditions[ ev.currentTarget ];
// Check if we need an active movieclip before
if( neededClickElement != null && ! _clicked[ neededClickElement ] )
return;
_clicked[ ev.currentTarget ] = true;
ev.currentTarget.alpha = 0.1;
}
}
}

AS3 Flash CS6 access of undefined properties? Help will be much obliged

Properties such as score1; score2, lives1, lives2 and STAGE seem to be undefined properties. I dont see why? Please help...
e.g.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 429 1120: Access of undefined property score1.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 430 1120: Access of undefined property score1.
package {
import flash.display.Stage;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.DropShadowFilter;
import flash.text.AntiAliasType;
import flash.text.Font;
public class Game{
//Global Declarations
private var STAGE:Stage;
'Graphics'
private var Title:Title_mc;
private var Score_lbl:Label_Score_txt;
private var Lives_lbl:Label_Lives_txt;
private var holes:MoleHoles_mc;
private var Lives_txt:TextField;
private var Score_txt:TextField;
private var Shadow:DropShadowFilter;
private var Pause_btn:Button_Pause;
'Game Properties'
private var SLEEP:Timer;
private var countdown:Timer;
private var countdownComplete:Timer;
private var countdownDelay:Timer;
private var count:TextField;
private var count_inc:int;
private var Paused:Boolean;
private var moles:int;
public static var mole_spawn_delay:int = 1000;
public static var mole_death_delay:int = 1000;
public static var molePlacable:Boolean = true;
private var unavailableHole:int;
public static var molesName:String;
private var lvl:int = 51;
private var score1:Anim_Score1_mc;
private var score2:Anim_Score2_mc;
private var lives1:Anim_Lives1_mc;
private var lives2:Anim_Lives2_mc;
'Player Properties'
public static var Score:int = 0;
public static var Lives:int = 3;
public static var incrementer:int = 1;
public function Game(STAGE:Stage) {
this.STAGE = STAGE;
//Enable Touch Events
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Instantiate Objects
Shadow = new DropShadowFilter();
//Handle new frames
STAGE.addEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
//Draw Graphics
'Menu Title'
Title = new Title_mc();
Title.x = 250.5;
Title.y = 62.35;
STAGE.addChild(Title);
'Content'
holes = new MoleHoles_mc();
holes.x = 0;
holes.y = 123.9;
STAGE.addChild(holes);
'Score Label'
Score_lbl = new Label_Score_txt();
Score_lbl.x = 19.65;
Score_lbl.y = 146.75;
STAGE.addChild(Score_lbl);
'Lives Label'
Lives_lbl = new Label_Lives_txt();
Lives_lbl.x = 358.25;
Lives_lbl.y = 141.05;
STAGE.addChild(Lives_lbl);
'Score Value'
Score_txt = new TextField();
Score_txt.x = 19.65;
Score_txt.y = 209.95;
Score_txt.width = 100;
Score_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Score_txt.selectable = false;
Score_txt.text = String(Score);
STAGE.addChild(Score_txt);
'Lives Value'
Lives_txt = new TextField();
Lives_txt.x = 410;
Lives_txt.y = 204.70;
Lives_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Lives_txt.selectable = false;
Lives_txt.text = String(Lives);
STAGE.addChild(Lives_txt);
'Pause Button'
Pause_btn = new Button_Pause();
Pause_btn.x = 22.40;
Pause_btn.y = 772.85;
STAGE.addChild(Pause_btn);
Pause_btn.buttonMode = true;
Pause_btn.addEventListener(TouchEvent.TOUCH_BEGIN, handle_Pause);
//Start Countdown
countDown();
}
//Pause/resume game
private function handle_Pause(e:TouchEvent):void
{
//...
}
//Sleep Method
private function sleep(seconds:int):void
{
SLEEP = new Timer(seconds, 1); // 1 second
SLEEP.addEventListener(TimerEvent.TIMER_COMPLETE, sleep_end);
SLEEP.start();
STAGE.frameRate = 0;
}
//Sleep Method Complete
private function sleep_end(e:TimerEvent):void
{
SLEEP.removeEventListener(TimerEvent.TIMER_COMPLETE , sleep_end);
STAGE.frameRate = 24;
}
//Count Down Timer
private function countDown():void
{
Paused = true;
count_inc = 5;
count = new TextField();
count.x = 213.9;
count.y = 158.05;
count.height = 150;
count.width = 150;
count.defaultTextFormat = new TextFormat("Balloonist", 150, 0xFFFFFF);
count.filters = [Shadow];
count.antiAliasType = AntiAliasType.ADVANCED;
count.sharpness = 400;
count.text = String(count_inc);
STAGE.addChild(count);
countdownComplete = new Timer(5000, 1);
countdownComplete.addEventListener(TimerEvent.TIMER, coutdown_Complete);
countdownDelay = new Timer(100);
countdownDelay.addEventListener(TimerEvent.TIMER, countDown_Tick);
countdown = new Timer(1000);
countdown.addEventListener(TimerEvent.TIMER, countDown_end);
countdownComplete.start();
countdownDelay.start();
countdown.start();
}
//Handle countdown tick
private function countDown_Tick(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdown.stop();
countdown.removeEventListener(TimerEvent.TIMER, countDown_end);
}else {
countdownDelay.delay = 100;
}
}
//Handle countown complete
private function countDown_end(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdownDelay.stop();
countdownDelay.removeEventListener(TimerEvent.TIMER, countDown_Tick);
}else{
count_inc -= 1;
count.text = String(count_inc);
}
}
//Countdown cleanup
private function coutdown_Complete(e:TimerEvent):void
{
STAGE.removeChild(count);
Paused = false;
}
//Main Game Loop
private function handle_ENTER_FRAME(e:Event):void
{
//Update game stuff
if(!Paused)
{
if(molePlacable)
{
sleep(mole_spawn_delay);
newMole();
}
Score_txt.text = String(Score);
Lives_txt.text = String(Lives);
}
//Clear stage & display game over interface
if(Lives <= 0)
{
STAGE.removeEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
STAGE.removeChild(Title);
STAGE.removeChild(holes);
STAGE.removeChild(Score_lbl);
STAGE.removeChild(Lives_lbl);
STAGE.removeChild(Pause_btn);
STAGE.removeChild(Score_txt);
STAGE.removeChild(Lives_txt);
var gameOver:GameOver = new GameOver(STAGE);
}
//Update mole stats
if(moles > 50)
{
lvl = 71;
}
//Dissallow score to go below 0
if(Score < 0)
{
Score = 0;
}
}
//Create new Mole
private function newMole():void
{
'Specify mole hole'
var rnd = Math.ceil(Math.random()*11);
'Ensure mole does not spawn from preceding hole'
while(rnd == unavailableHole)
{
rnd = Math.ceil(Math.random()*11);
}
var X:int;
var Y:int;
switch(rnd)
{
case 0:
X = -14.75;
Y = 293.45;
break;
case 1:
X = 109.25;
Y = 291.35;
break;
case 2:
X = 223.75;
Y = 291.35;
break;
case 3:
X = 337.2;
Y = 291.35;
break;
case 4:
X = 0;
Y = 430;
break;
case 5:
X = 118.7;
Y = 430;
break;
case 6:
X = 226.9;
Y = 430;
break;
case 7:
X = 342.45;
Y = 430
break;
case 8:
X = 0;
Y = 561.35
break;
case 9:
X = 112.4;
Y = 561.35;
break;
case 10:
X = 229;
Y = 561.35;
break;
case 11:
X = 339.3;
Y = 561.35;
break;
}
'Specify molde to add'
rnd = lvl * Math.random();
if(rnd <=40)
{
//Default + 10*incrementer
var mole:Mole_Default_mc = new Mole_Default_mc(STAGE, X, Y);
}else if(rnd <=42){
//Crazy - 5*incrementer
var mole2:Mole_Crazy_mc = new Mole_Crazy_mc(STAGE, X, Y);
}else if(rnd <=43){
//Crazy2 - 10*inrementer
var mole3:Mole_Crazy2_mc = new Mole_Crazy2_mc(STAGE, X, Y);
}else if(rnd <45){
//Lady + 1 life
var mole4:Mole_Lady_mc= new Mole_Lady_mc(STAGE, X, Y);
}else if(rnd <46){
//Ninja - 2*inrementer
var mole5:Mole_Ninja_mc = new Mole_Ninja_mc(STAGE, X, Y);
}else if(rnd <47){
//Zombie + 5 * lives
var mole6:Mole_Zombie_mc = new Mole_Zombie_mc(STAGE, X, Y);
}else if(rnd <48){
//reaper - Lives
var mole7:Mole_Reaper_mc = new Mole_Reaper_mc(STAGE, X, Y);
}else if(rnd <49){
//Snob + 250
var mole8:Mole_Snob_mc = new Mole_Snob_mc(STAGE, X, Y);
}else if(rnd <52){
//Angel + 3 lives
var mole9:Mole_Angel_mc = new Mole_Angel_mc(STAGE, X, Y);
}else if(rnd <54){
//Demon - 3 lives
var mole10:Mole_Demon_mc = new Mole_Demon_mc(STAGE, X, Y);
}else if(rnd <55){
//Creature - 3+incrementer
var creature:Mole_Creature_mc = new Mole_Creature_mc(STAGE, X, Y);
}else if(rnd <56){
//Cyber + 50+incrementer
var cyber:Mole_Cyber_mc = new Mole_Cyber_mc(STAGE, X, Y);
}else if(rnd <57){
//Grumpy + 5
var grumpy:Mole_Grumpy_mc = new Mole_Grumpy_mc(STAGE, X, Y);
}else if(rnd <58){
//Hippie Lives+3 Score+100
var hippie:Mole_Hippie_mc = new Mole_Hippie_mc(STAGE, X, Y);
}else if(rnd<59){
//Hyper 30*incrementer
var hyper:Mole_Hyper_mc = new Mole_Hyper_mc(STAGE, X, Y);
}else if(rnd<60){
//Love timer-100
var love:Mole_Love_mc = new Mole_Love_mc(STAGE, X, Y);
}else if(rnd<61){
//LoveZombie - 20*Lives
var loveZombie:Mole_LoveZombie_mc = new Mole_LoveZombie_mc(STAGE, X, Y);
}else if(rnd<70){
//Sleepy Timer+100
var sleepy:Mole_Sleepy_mc = new Mole_Sleepy_mc(STAGE, X, Y);
}else if(rnd<71){
//Warrior + (10*incrementer)*2
var warrior:Mole_Warrior_mc = new Mole_Warrior_mc(STAGE, X, Y);
}
//Update mole stats
moles += 1;
if(mole_spawn_delay > 20 && mole_death_delay > 20)
{
mole_spawn_delay -= 10;
mole_death_delay -= 5;
}
//Update incrementer
if(moles > 100)
{
incrementer = 50;
}else if(moles > 80)
{
incrementer = 40;
}else if(moles > 60)
{
incrementer = 30;
}else if(moles > 20)
{
incrementer = 20;
}else if(moles > 10)
{
incrementer = 10;
}
}
//Animation
public static function animate(type:int)
{
if(type == 0)
{
score1 = new Anim_Score1_mc();
score1.x = 40;
score1.y = 250.6;
STAGE.addChild(score1);
var anim_timer:Timer = new Timer(2000, 1);
anim_timer.addEventListener(TimerEvent.TIMER, remove_score1);
anim_timer.start();
}else if(type == 1)
{
score2 = new Anim_Score2_mc();
score2.x = 32;
score2.y = 248.6;
STAGE.addChild(score2);
var anim_timer2:Timer = new Timer(2000, 1);
anim_timer2.addEventListener(TimerEvent.TIMER, remove_score2);
anim_timer2.start();
}else if(type == 2)
{
lives1 = new Anim_Lives1_mc();
lives1.x = 430.9;
lives1.y = 237.95;
STAGE.addChild(lives1);
var anim_timer3:Timer = new Timer(2000, 1);
anim_timer3.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer3.start();
}else{
lives2 = new Anim_Lives2_mc();
lives2.x = 430.9;
lives2.y = 237.95;
STAGE.addChild(lives2);
var anim_timer4:Timer = new Timer(2000, 1);
anim_timer4.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer4.start();
}
}
//Handle remove_score1
private function remove_score1(e:TimerEvent):void
{
STAGE.removeChild(score1);
}
//Handle remove_score2
private function remove_score2(e:TimerEvent):void
{
STAGE.removeChild(score2);
}
//Handle remove_lives1
private function remove_lives1(e:TimerEvent):void
{
STAGE.removeChild(lives1);
}
//Handle remove_lives2
private function remove_lives2(e:TimerEvent):void
{
STAGE.removeChild(lives2);
}
}
}
Replace:
public static function animate(type:int)
with
public function animate(type:int)
Or if you want to have that method static add:
private static var _instance:Game = null;
in Game Class definition and change animate function:
public static function animate(type:int):void{
if(_instance!=null) _instance.hidden_animate(type);
}
protected function hidden_animate(type:int){
// PLACE HERE CODE FROM YOUR ORIGINAL ANIMATE FUNCTION
}
in constructor function add:
_instance = this;
If you often use static methods (like in this code) read more bout design patterns like singleton :)

Error 2025 when trying to removeChild, child not contained where it was added?

I am trying to code a tile-based level editor, in which the Main class adds Tile class instances as children of the 'tiles' movieclip when clicking/dragging the mouse.
I am able to add tiles to the container, and they show on stage, however I cannot remove any tiles when erasing them is enabled. It gives me the following error
Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/Draw()
at Main/::Go()
Also, when I check if the tile is inside the tiles container, it tells me that the parent is null.
So, a little help? I tried checking other questions with similar issues but none seemed to be close to mine.
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
//import flash.events.TimerEvent.updateAfterEvent;
public class Main extends MovieClip {
//containers
var lines:Sprite = new Sprite();
var tiles:Sprite = new Sprite();
// Grid data
var tileW:int = 20;
var tileH:int = 20;
var gridW:int = 20;//(inputWidth);
var gridH:int = 20;//(inputHeight);
var gridX:int = 50;
var grixY:int = 50;
var level:Array;
//Drawing variables
var go:Boolean = false;
var erase:Boolean = false;
var default_tile:int = 1;
var type:int;
var rect:Object = {x:100, y:50, width:(gridW * tileW)/100, height:(gridH * tileH)/100};
//menus
var sizeMenu:SizeMenu = new SizeMenu();
var current:Tile = new Tile();
public function Main():void {
//Flash alignment and resizing
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.addChild(lines);
lines.x = rect.x;
lines.y = rect.y;
stage.addChild(tiles);
tiles.x = rect.x;
tiles.y = rect.y;
stage.addChild(sizeMenu);
stage.addChild(current);
current.x = 50;
current.gotoAndStop(default_tile);
stage.addEventListener(MouseEvent.MOUSE_DOWN, Go);
stage.addEventListener(MouseEvent.MOUSE_UP, Go);
stage.addEventListener(MouseEvent.MOUSE_MOVE, Go);
stage.addEventListener(KeyboardEvent.KEY_DOWN, ToggleErase);
stage.addEventListener(KeyboardEvent.KEY_UP, ToggleErase);
Setup();
}
//Draws grid lines
private function Setup():void {
trace("Drawing Grid...");
// create an empty array
level = new Array(gridH);
for (var i=0; i < gridW; i++) {
level[i] = new Array(gridW);
}
// attach lines to create a grid
for (var k=0; k <= gridH; k++) {
var line = new Line();
line.name = "line"+k;
line.scaleX = rect.width;
line.y = tileH * k;
lines.addChild(line);
for (var j=0; j <= gridW; j++) {
line = new Line();
line.name = "line"+j+"_"+k;
line.scaleX = rect.height;
line.x = tileW * j;
line.rotation = 90;
lines.addChild(line);
}
}
type = default_tile;
trace("Done drawing grid!");
}
//Decided if drawing is possible
private function Go(e:MouseEvent):void {
if (e.type == "mouseDown") {
go = true;
Draw(e);
}
if (e.type == "mouseUp") {
go = false;
}
if (e.type == "mouseMove") {
if (go) {
Draw(e);
}
//e.updateAfterEvent();
}
}
//Toggles erase
private function ToggleErase(e:KeyboardEvent):void{
if (e.shiftKey){
erase = true;
}
if (e.type == "keyUp"){
erase = false;
}
}
// attaches the tiles when drawn on the grid
public function Draw(e:MouseEvent) {
var x = mouseX;
var y = mouseY;
var cx = Math.floor((x - rect.x) / tileW);
var cy = Math.floor((y - rect.y) / tileH);
if (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH) {
var target = e.currentTarget;
if (!erase) {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
level[cy][cx] = default_tile - 1;
}
}
}
//Cleans the grid and redraws it
private function ResetGrid():void {
level = null;
//Delete tiles
while (tiles.numChildren) {
tiles.removeChildAt(0);
}
//Delete lines
while (lines.numChildren) {
lines.removeChildAt(0);
}
gridW=20;
gridH=20;
rect.width = (gridW * tileW)/100;
rect.height = (gridH * tileH)/100;
}
// updates the current-clip
private function update() {
current.gotoAndStop(type);
}
}
}
The following code is causing the problem, basically you are calling removeChild twice for the same object.
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
tiles.removeChild(target);
In your code, I notice that you have the ability for the tile to be removed, and then try to remove it again at the end of this block :
if (!erase) {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (tiles.contains(target)){
trace("Contained!");
tiles.removeChild(target);
}
// this is going to throw an error
tiles.removeChild(target);
Make it easy on yourself and create a class for Tile with a remove method. Something like this:
class Tile extends Sprite
{
public function remove():void
{
if(parent) parent.removeChild(this);
}
}
This way, you can simply do:
tile.remove();
I have resolved my issue! All tile instances are given a name based on their position on the grid when added. Instead of making target the object the mouse was pointing at, I used getChildByName(); to search if there was already an object with a specific name, and to erase it if it did.
if (cx >= 0 && cx < gridW && cy >= 0 && cy < gridH) {
var target = tiles.getChildByName("t_" + cy + "_" + cx);
if (!erase) {
if (target){
tiles.removeChild(target);
}
var tile = new Tile();
tiles.addChild(tile);
tile.name = ("t_" + cy + "_" + cx);
tile.x = (tileW * cx);
tile.y = (tileH * cy);
tile.gotoAndStop(type);
level[cy][cx] = type;
} else {
if (target){
tiles.removeChild(target);
}
level[cy][cx] = default_tile - 1;
}
}