Reputation: 13
I'm new to AS3, and I'm having a stupidly small issue that I can't get past.
I have a scene where, when I click on the stage, I generate a MovieClip box and place it on stage in a grid fashion. I want to click on it again to remove it, but it's this part that's not working. I think I narrowed it down to the removeChild()
script itself, because in AS3, I get DisplayObject
errors... But, that's just what I've found, and I might be wrong. For the most part, I'm testing using the 'WonderFL' website.
package {
import flash.display.AVM1Movie;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
//Begin Code
//Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, stageClick);
//Variables
var squareSize:Number = 120;
var xGridPos:Number = 0;
var yGridPos:Number = 0;
var tallyContainer:MovieClip = new MovieClip();
//Clicking on the stage
function stageClick(me:MouseEvent):void {
xGridPos = Math.floor(stage.mouseX/squareSize) * squareSize;
yGridPos = Math.floor(stage.mouseY/squareSize) * squareSize;
createTally();
}
//Clicking on a tally
function tallyDown(me:MouseEvent):void {
removeChild(tally);
var visibleTrace:MovieClip = new MovieClip();
visibleTrace.graphics.beginFill (0x000000, 1) ;
visibleTrace.graphics.drawRect (0, 0, 20, 20);
visibleTrace.graphics.endFill();
addChild(visibleTrace);
}
//Creating a tally
function createTally():void {
var tally:MovieClip = new MovieClip();
tally.graphics.beginFill(0xDD1111, 1) ;
tally.graphics.drawRect(xGridPos, yGridPos, squareSize, squareSize) ;
tally.graphics.endFill() ;
tally.addEventListener(MouseEvent.MOUSE_DOWN, tallyDown);
addChild(tally);
}
//End Code
}
}
}
I tried looking up solutions (there have been other posts on Stack Overflow about apples and bears), but I couldn't successfully implement them. I also looked up Arrays and Loops (specifically, the 'Basix' tutorials on code.tutsplus). Again, I couldn't successfully implement them.
What am I doing wrong with deleting the tallys?
Upvotes: 1
Views: 35
Reputation: 9839
Besides the DisplayObject
errors, you have some little problems in your code, let's see them.
Firstly, you can not put all your code inside your class's constructor, so your class can be like this :
package {
// imports
public class FlashTest extends Sprite
{
// properties and constants declaration part
// constructor
public function FlashTest()
{
// class's initialization instructions and other stuff
}
// other methods
}
}
Then, the tally
object created inside the createTally()
function ( var tally:MovieClip = new MovieClip();
) it's its own local object which is inaccessible (and of course undefined
) outside it, so other functions can not use it.
But in your case, you don't even need to get access to that object, because to get your current clicked object, you can use the target
property of your MouseEvent
instance :
function tallyDown(me:MouseEvent):void
{
var tally:MovieClip = MovieClip(me.target);
// then to remove that "tally" :
tally.parent.removeChild(tally);
// ...
}
...
BTW, you can use the free FlashDevelop to test your code ...
Hope that can help.
Upvotes: 1