Flash remove child from timer - actionscript-3

I have 25 objects of movie clip class named drone, and when i click it, after 2 seconds I want the object to disappear. I also have 25 timers named countdown. Here is what i do:
function clickHandler (event:MouseEvent):void{
event.currentTarget.hp--;
if(event.currentTarget.hp <= 0)
{
for(var i:int = 0;i<25;i++)
{
if(event.currentTarget == _drone[i])
{
countdown[i].start(); //start timer
}
}
}
}
Here is my timer:
for(var i:int = 0;i<25;i++)
{
countdown[i] = new Timer(2000);
countdown[i].addEventListener(TimerEvent.TIMER,timerHandler);
}
function timerHandler(e:TimerEvent):void {
//remove the drone I clicked
//I also dont know which drone i'm clicking
}
What should I do in the timerHandler to remove the object I clicked?

You can use Dictionary. Use the timer as key and movielcip as value.
import flash.utils.Dictionary;
var dict:Dictionary = new Dictionary();
function clickHandler (event:MouseEvent):void{
event.currentTarget.hp--;
if(event.currentTarget.hp <= 0)
{
for(var i:int = 0;i<25;i++)
{
if(event.currentTarget == _drone[i])
{
dict[countdown[i]] = _drone[i];//set the target mc here
countdown[i].start(); //start timer
break;
}
}
}
}
function timerHandler(e:TimerEvent):void {
var mc:MovieClip = dict[e.target] as MovieClip;//get the object been clicked
if (mc && mc.parent) {
mc.parent.removeChild(mc);//remove it
}
}

With minimal changes, set up an array to track the drones:
var arrayToRemove:Array = new Array();
and then in the click handler store drones to be removed in there:
arrayToRemove.push(event.currentTarget);
and in the timerHandler just remove the first element of the array:
removeChild(arrayToRemove.shift());
Since every delay is the same the order of the events and removals will be preserved. Although, it would probably be better to generalize the code using the above example and store all drones and timers in an arrays, so you can have any number of them.

Related

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

I am trying to get rid of an object but it's throwing an error not exactly sure what is the problem.
The error I am getting:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at SchoolBook_Final_fla::MainTimeline/garbage_cl_2()[SchoolBook_Final_fla.MainTimeline::frame2:98]
at Function/SchoolBook_Final_fla:MainTimeline/choice_play_ev/SchoolBook_Final_fla:play_target()[SchoolBook_Final_fla.MainTimeline::frame2:81]
My code:
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.Event;
var week_pick:Weekday_Choice = new Weekday_Choice ();
var weekdayArray: Array = new Array ();
var monday:Mon_D_but = new Mon_D_but ();
var tuesday:Tues_D_but = new Tues_D_but ();
var wednesday:Wed_D_but = new Wed_D_but ();
var thursday:Thurs_D_but = new Thurs_D_but ();
var friday:Fri_D_but = new Fri_D_but ();
var choice_play: Array = new Array ();
choice_play.push(monday, tuesday, wednesday, thursday, friday);
//Adding Week Pick
addChild(week_pick);
//Placing all the Buttons
choice_play[0].x = 73.1;
choice_play[0].y = 316.75;
choice_play[1].x = 251.9;
choice_play[1].y = 278.35;
choice_play[2].x = 399.95;
choice_play[2].y = 375.85;
choice_play[3].x = 345.2;
choice_play[3].y = 602.4;
choice_play[4].x = 80.15;
choice_play[4].y = 603.05;
for (var a = 0; a < choice_play.length; a++)
{
//Asking all the Buttons to stop
choice_play[a].alpha = 0;
choice_play[a].stop();
}
//Event for the week pick to start
week_pick.addEventListener(Event.ENTER_FRAME, moveon);
//Getting the function started
function moveon(event:Event)
{
if (week_pick.currentFrame == 103)
{
week_pick.stop();
for (a = 0; a < choice_play.length; a++)
{
addChild(choice_play [a]);
}
//adding an Event listener for clicking the buttons
this.addEventListener(MouseEvent.CLICK,choice_play_ev);
}
}
function choice_play_ev(play_event: MouseEvent)
{
trace ("Event gets added");
trace (play_event.target);
//Work if the target isn't the background clip
if (play_event.target != week_pick)
{
trace (play_event.target);
//Turn back the opacity for what ever was cliked on
play_event.target.alpha = 1;
//asking the choice to play it's animation
play_event.target.addEventListener(Event.ENTER_FRAME, play_target);
this.removeEventListener(MouseEvent.CLICK,choice_play_ev);
function play_target(play_event_cl:Event)
{
play_event_cl.target.play ();
if (play_event_cl.target.currentFrame == 15)
{
//remove the [;ace event listener once the animation is done
play_event.target.removeEventListener(Event.ENTER_FRAME, play_target);
garbage_cl_2 ();
}
}
}
}
function garbage_cl_2 ()
{
trace("it works");
for (a = 0; a < choice_play.length; a++)
{
//remove all the choices from the display list
removeChild(choice_play [a]);
}
//Remove all the choices from the array for better garbage cleaning
choice_play.splice(0, choice_play.length);
removeChild(week_pick);
week_pick.removeEventListener(Event.ENTER_FRAME, moveon);
trace("The choice_play is " + choice_play.length);
gotoAndStop(3);
}
Can somebody help me out with this please? Also tell me why is it throwing the error?
Your loop inside the garbage_cl_2 () function
for (a = 0; a < choice_play.length; a++)
{
//remove all the choices from the display list
removeChild(choice_play [a]);
}
and / or the call removeChild(week_pick); tries to remove a MovieClip that's not direct child of the current MovieClip object. The current clip is the one whose timeline contains the frame with the code of the garbage_cl_2 () function. The removeChild() method can only remove a direct child of a DisplayObjectContainer instance - if the current clip is not the direct parent of these clips, you need to specify the parent clip like so:
parentclip.removeChild ( ... );

Drawing app undo and redo function action script 3

Can anyone show me how can I make undo and redo function? so this is my current action script. I cant figure how to do it and i see some example in some web site, the action script is to long to under stand. Pls show a simple way that i can make this work..
sorry for bad grammar...
var drawingLine:Shape=new Shape();
board.addChild(drawingLine);
var doDraw:Boolean=false;
var lineSize:Number=7;
var activeColor:uint = 0x000000;
function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}
function MouseDown(e:MouseEvent):void{
doDraw=true;
drawingLine.graphics.moveTo(drawingLine.mouseX, drawingLine.mouseY);
drawingLine.graphics.lineStyle(lineSize, activeColor);
board.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
}
function MouseMove(e:MouseEvent):void{
var curX:Number=drawingLine.mouseX;
var curY:Number=drawingLine.mouseY;
if(doDraw && checkCoords(curX,curY)){
if(active=="Line"){
clearTemp();
temporaryDrawing.graphics.lineTo(curX,curY);
}else{
drawingLine.graphics.lineTo(curX,curY);
}
e.updateAfterEvent();
}
}
function MouseUp(event:MouseEvent):void{
doDraw=false;
}
btn_Pencil.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
this,o Using two graphics,would not be the way to go
The way I would approach this is by creating a lineInfo class (seeing how you are using lines only), and have the information of startpoint, endpoint, lineColour, lineWidth, and lineAlpha .
Then create an array or Vector and populate it with a new LineInfo class, and update the graphics with the line. If you need to undo, you can set the last item use value to false, and redraw the graphics from the instruction of the array. Create a undo step integer, that keeps track of how many (counting from the back) actions should be omitted.
To improve performance, you can create a maximum of 25 instructions, and create a graphics that caches the items not in the undo list.
You could save all movements after clicks in an array or vector, then if you want to undo, you redraw all movements in the array except the last one.
Could be something like this:
var drawingLine:Shape=new Shape();
board.addChild(drawingLine);
// MOVEMENTS INFORMATION
var movements:Array = new Array();
var doDraw:Boolean = false;
var cacheIndex:int = 0;
var lineSize:Number=7;
var activeColor:uint = 0x000000;
function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}
function MouseDown(e:MouseEvent):void{
function PencilTool(event:MouseEvent):void{
board.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
board.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
}
function MouseDown(e:MouseEvent):void{
doDraw=true;
drawingLine.graphics.moveTo(drawingLine.mouseX, drawingLine.mouseY);
drawingLine.graphics.lineStyle(lineSize, activeColor);
board.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
lastTracing = {
mainPoint: {
x:drawingLine.mouseX,
y:drawingLine.mouseY
},
lineSize: lineSize,
activeColor:activeColor,
points:new Array()
}
cacheIndex = 0;
movements.splice(movements.length - cacheIndex, cacheIndex);
movements.push(lastTracing );
}
function MouseMove(e:MouseEvent):void{
var curX:Number=drawingLine.mouseX;
var curY:Number=drawingLine.mouseY;
if(doDraw && checkCoords(curX,curY)){
if(active=="Line"){
clearTemp();
temporaryDrawing.graphics.lineTo(curX,curY);
}else{
drawingLine.graphics.lineTo(curX,curY);
lastTracing.points.push({x:curX, y:curY});
}
e.updateAfterEvent();
}
}
function MouseUp(event:MouseEvent):void{
doDraw=false;
}
function undoFunction(event:MouseEvent=null):void {
if(cacheIndex+1 <= movements.length){
cacheIndex++;
}
drawCache();
}
function redoFunction(event:MouseEvent = null):void {
if(cacheIndex-1 >= 0){
cacheIndex--;
}
drawCache();
}
function drawCache():void {
for(var i:uint=0;i<(movements.length-cacheIndex);i++){
var tracingInfo:Object = movements[i];
drawingLine.graphics.clear();
drawingLine.graphics.moveTo(tracingInfo.mainPoint.x, tracingInfo.mainPoint.y);
drawingLine.graphics.lineStyle(tracingInfo.lineSize, tracingInfo.activeColor);
for(var j:uint=0;j<tracingInfo.points.length;j++){
drawingLine.graphics.lineTo(tracingInfo.points[j].x,tracingInfo.points[j].y);
}
}
}
btn_Pencil.addEventListener(MouseEvent.MOUSE_UP, PencilTool);

Error #1010 after splicing from array

I have an issue here. Every five seconds the spawnEnemies is fired. It creates a movieclip, sets it position and adds it to the stage and an array. In the function checkBullet I check if the bullet hits one of the enemies. If it does, the bullet and the enemy are removed from the stage, and the enemy is spliced from the array. But an error is thrown once I hit an enemy:
TypeError: Error #1010: A term is undefined and has no properties.
If I comment out the arrayEnemies.splice(i,1) line, it works fine but then it isn't removed from the array. This is in itself not an issue, but naturally I don't like to keep unnecessary data in my array. Any help on this?
function checkBullet(event:Event) {
if(stage.contains(mcBullet)) {
for(var i:int = 0; i < arrayEnemies.length; i++) {
if(arrayEnemies[i].hitTestPoint(mcBullet.x, mcBullet.y, true)) {
stage.removeChild(mcBullet);
this.removeChild(arrayEnemies[i]);
arrayEnemies.splice(i,1);
bulletOnStage = false;
}
}
if(mcBullet.x > 800) {
stage.removeChild(mcBullet);
bulletOnStage = false;
}
}
}
function spawnEnemies(event:TimerEvent) {
var enemie:MovieClip = new box_mc();
enemie.name = "mcBox" + event.currentTarget.currentCount;
enemie.x = 850;
enemie.y = Math.floor(Math.random()*(1+155)+255);
addChild(enemie);
arrayEnemies.push(enemie);
}
function moveEnemies(event:Event) {
for(var i:int = 0; i < arrayEnemies.length; i++) {
arrayEnemies[i].x -= 5;
}
}
This will be caused by working on an array that you are interating through, you should hold a ref to the stuff you want to remove then do it after the loop.
Your problem is that if the bullet hits two enemies, you try to remove it from the stage twice. This will throw an ArgumentError.
If you need to test against all enemies (assuming multiple enemies can be hit by the same bullet), you also need to decrement i when you remove an element from your enemy array.
function checkBullet(event:Event) {
if(stage.contains(mcBullet)) {
if(mcBullet.x > 800) {
stage.removeChild(mcBullet);
bulletOnStage = false;
}
for(var i:int = 0; i < arrayEnemies.length; i++) {
if(arrayEnemies[i].hitTestPoint(mcBullet.x, mcBullet.y, true)) {
if(stage.contains(mcBullet)){
stage.removeChild(mcBullet);
}
this.removeChild(arrayEnemies[i]);
arrayEnemies.splice(i,1);
bulletOnStage = false;
i--;
}
}
}
}
Bit of an older question but thought I'd throw in my answer too for anyone that might end up here.
Like Neil said, editing an array that you're itterating through (in this case arrayEnemies) can cause concurrent update problems.
My prefered solution is to use a seperate toRemove array and remove them after the update, for example:
var enemies:Array();
//Lets assume at some point this is populated with Enemy objects
function update():void
{
var toRemove:Array = new Array();
//May want to keep and clear an array instead to avoid creating a new one each update
foreach(var enemy:Enemy in enemies)
{
enemy.update();
if(enemy.markedForRemoval())
toRemove.push(enemy);
}
foreach(var enemy:Enemy in toRemove)
enemies.splice(enemies.indexOf(enemy), 1);
}

Add an array of Movie Clips to a filter ActionScript 3

i stored 8 mc's in an array an i put them on the stage.
now I want to apply to these mc's a blur effect.
My problem is that i don't know how to apply for every mc the blur effect by clicking on it.
So for example I have all the mc's on the stage and if I click on one of them the clicked one should have the blur effect and so on.
How to apply the filter to the mc's?
Thank you for you time
You can loop through the mc's array and add event listeners on each of them:
var mcArrayLength:int = mcArray.length();
for (var i:int = 0; i < mcArrayLength; i++) {
var mc:MovieClip = mcArray[i] as MovieClip;
mc.addEventListener(MouseEvent.CLICK, onMcClick);
}
and the event handler:
function onMcClick(e:MouseEvent):void
{
var clickedMc:MovieClip = e.currentTarget as MovieClip;
clickedMc.filters = [myBlurFilter];
}
of course, if you wanted to have only one blurred mc at a time, you should keep a reference to it and remove blur once another mc is clicked:
var currentlyClickedMc:MovieClip;
function onMcClick(e:MouseEvent):void
{
var clickedMc:MovieClip = e.currentTarget as MovieClip;
clickedMc.filters = [myBlurFilter];
if (currentlyClickedMc) currentlyClickedMc.filters = [];
currentlyClickedMc = clickedMc;
}
something like this (untested) should work:
package
{
//Imports
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
//Class
public function ClickToBlur
{
//Variables
private var clickableObjects:Array;
//Constructor
public function ClickToBlur(clickableObjects:Array)
{
this.clickableObjects = clickableObjects;
init();
}
//Initialize
private function init():void
{
for each (var object:Object in clickableObjects)
{
object.addEventListener(MouseEvent.CLICK, clickMouseEventHandler);
}
}
//Click Mouse Event Handler
private function clickMouseEventHandler(evt:MouseEvent):void
{
if (evt.currentTarget.filters == [])
{
evt.currentTarget.filters = [new BlurFilter()];
}
else
{
evt.currentTarget.filters = [];
}
}
//Dispose
public function dispose():void
{
for each (var object:Object in clickableObjects)
{
object.removeEventListener(MouseEvent.CLICK, clickMouseEventHandler);
}
}
}
}
just pass your array to a new instance of the class
var ctb:Object = new ClickToBlur(myArrayOfObjects);
and clean up when your done:
ctb.dispose();
since they're all Movie Clips, you should use a Vector instead of an Array.

Multiple movieclips all go to the same spot; What am i doing wrong?

So I'm trying to shoot multiple bullets out of my body and it all works except I have an odd problem of just one bullet showing up and updating to set position for the new ones.
I have a move able player thats supposed to shoot and I test this code by moving the player and shooting. Im taking it step by step in creating this.
The result of tracing the bulletContainer counts correctly in that its telling me that movieclips ARE being added to the stage; I Just know it comes down to some kind of logic that im forgetting.
Here's My Code (The Bullet it self is a class)
UPDATE*
Everything in this code works fine except for I stated earlier some code seems reduntned because I've resorted to a different approaches.
BulletGod Class:
public class bulletGod extends MovieClip{
//Register Variables
//~Global
var globalPath = "http://127.0.0.1/fleshvirusv3/serverside/"
//~MovieCLips
var newBullet:bulletClass = new bulletClass();
//~Boolean
var loadingBulletInProgress:Number = 0;
var shootingWeapon:Number = 0;
//~Timers
var fireBulletsInterval = setInterval(fireBullets, 1);
var bulletFireEvent;
//~Arrays
var bulletArray:Array = new Array();
var bulletType:Array = new Array();
var bulletContainer:Array = new Array();
//~Networking
var netBulletRequest:URLRequest = new URLRequest(globalPath+"bullets.php");
var netBulletVariables:URLVariables = new URLVariables();
var netBulletLoader:URLLoader = new URLLoader();
//~Bullet Image Loader
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest();
public function bulletGod() {
//Load every bullet for every gun
//Compile data to be requested
netBulletVariables.act = "loadBullets"
netBulletRequest.method = URLRequestMethod.POST
netBulletRequest.data = netBulletVariables;
netBulletLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
netBulletLoader.addEventListener(Event.COMPLETE, getBulletImages);
netBulletLoader.load(netBulletRequest);
}
private function getBulletImages(bulletImageData:Event){
//Request every bullet URL image
//Set vars
var bulletData = bulletImageData.target.data;
//Load images
for(var i:Number = 0; i < bulletData.numBullets; i++){
bulletArray.push(bulletData["id"+i.toString()]);
bulletType.push(bulletData["bullet"+i.toString()]);
//trace(bulletData["id"+i]+"-"+bulletData["bullet"+i]);
}
//All the arrays have been set start firing the image loader/replacer
var imageLoaderInterval = setInterval(imageReplacer, 10);
}
private function imageReplacer(){
//Check to see which image needs replacing
if(!loadingBulletInProgress){
//Begin loading the next image
//Search for the next "String" in the bulletType:Array, and replace it with an image
for(var i:Number = 0; i < bulletType.length; i++){
if(getQualifiedClassName(bulletType[i]) == "String"){
//Load this image
mRequest = new URLRequest(globalPath+"ammo/"+bulletType[i]);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImage);
mLoader.load(mRequest);
//Stop imageReplacer() while we load image
loadingBulletInProgress = 1;
//Stop this for() loop while we load image
i = 999;
}
}
}
}
private function loadImage(BlackHole:Event){
//Image has loaded; find which array slot it needs to go into
for(var i:Number = 0; i <= bulletType.length; i++){
if(getQualifiedClassName(bulletType[i]) == "String"){
//We found which array type it belongs to; now replace the text/url location with the actual image data
var tmpNewBullet:MovieClip = new MovieClip;
tmpNewBullet.addChild(mLoader);
//Add image to array
bulletType[i] = tmpNewBullet;
//Restart loadingBullets if there are more left
loadingBulletInProgress = 0;
//Stop for() loop
i = 999;
}
}
}
//###############################################################################################################################################
private function fireBullets(){
//If player is holding down mouse; Fire weapon at rate of fire.
if(shootingWeapon >= 1){
if(bulletFireEvent == null){
//Start shooting bullets
bulletFireEvent = setInterval(allowShooting, 500);
}
}
if(shootingWeapon == 0){
//The user is not shooting so stop all bullets from firing
if(bulletFireEvent != null){
//Strop firing bullets
clearInterval(bulletFireEvent);
bulletFireEvent = null
}
}
}
private function allowShooting(){
//This function actually adds the bullets on screen
//Search for correct bullet/ammo image to attach
var bulletId:Number = 0;
for(var i:Number = 0; i < bulletArray.length; i++){
if(bulletArray[i] == shootingWeapon){
//Bullet found
bulletId = i;
//End For() loop
i = 999;
}
}
//Create new bullet
//Create Tmp Bullet
var tmpBulletId:MovieClip = new MovieClip
tmpBulletId.addChild(newBullet);
tmpBulletId.addChild(bulletType[bulletId]);
//Add To Stage
addChild(tmpBulletId)
bulletContainer.push(tmpBulletId); //Add to array of bullets
//Orientate this bullet from players body
var bulletTmpId:Number = bulletContainer.length
bulletTmpId--;
bulletContainer[bulletTmpId].x = Object(root).localSurvivor.x
bulletContainer[bulletTmpId].y = Object(root).localSurvivor.y
//addChild(bulletContainer[bulletTmpId]);
}
//_______________EXTERNAL EVENTS_______________________
public function fireBullet(weaponId:Number){
shootingWeapon = weaponId;
}
public function stopFireBullets(){
shootingWeapon = 0;
}
}
}
BulletClass:
package com{
import flash.display.*
import flash.utils.*
import flash.net.*
import flash.events.*
public class bulletClass extends MovieClip {
public var damage:Number = 0;
public function bulletClass() {
//SOME MOVEMENT CODE HERE
}
public function addAvatar(Obj:MovieClip){
this.addChild(Obj);
}
}
}
Well ... if I may say so, this code looks quite wrong. Either something is missing from the code or this code will never make the bullets fly.
First off, you can set x and y of the new bullet directly (replace everything after "orientate this bullet from players body" with this):
tmpBulletId.x = Object(root).localSurvivor.x;
tmpBulletId.y = Object(root).localSurvivor.y;
Perhaps this already helps, but your code there should already do the same.
But to let these bullets fly into any direction, you also need to add an event listener, like so:
tmpBulletId.addEventListener(Event.ENTER_FRAME, moveBullet);
function moveBullet(e:Event) {
var movedBullet:MovieClip = MovieClip(e.currentTarget);
if (movedBullet.x < 0 || movedBullet.x > movedBullet.stage.width ||
movedBullet.y < 0 || movedBullet.y > movedBullet.stage.height) {
// remove move listener, because the bullet moved out of stage
movedBullet.removeEventListener(Event.ENTER_FRAME);
}
// remove the comment (the //) from the line that you need
MovieClip(e.currentTarget).x += 1; // move right
// MovieClip(e.currentTarget).y -= 1; // move up
// MovieClip(e.currentTarget).x -= 1; // move left
// MovieClip(e.currentTarget).y += 1; // move down
}
This example lets your bullet fly to the right. If you need it flying into another direction, just comment out the line with the "move right" comment and uncomment one of the other lines.
This is of course a very simple example, but it should get you started.
I hope this helps, and that my answer is not the wrong answer to the question.
As far as I have expirienced it you can have only one copy of MovieClip object added to specific child. Best approach is to use ByteArray for the clip source and instantiate new MovieClip and pass the ByteArray as a source. It have something to do with child/parent relation since a DisplayObject can have only one parent (and a way to detach the object from scene too).
Well i ended up writeing the whole code from scratch for a 3rd time and ran into a similar problem and just for reference to anybody else that comes to a problem thats random as this one i found that problem was likly does to a conversion error somewhere that doesn't necessarily break any compiling rules. Just that i was calling a movieclip and not the class it self.