Why can't I remove this Movieclip? - actionscript-3

I have a TouchEvent function onTouch inside the constructor function of a item_Potion class that gets run when a Movieclip is touched. This function goes through a series of unrelated checks and then in the end it is supposed to remove itself (the Movieclip).
At the end of the onTouch function it is supposed to remove itself by doing the following:this.parent.removeChild(this);
However, this does not work.
I get the following error message:
TypeError: Error #1010: A term is undefined and has no properties.
at Function/item_Potion/$construct/onTouch()[E:\Clients\org\tcdsb\ZenithsReach\item_Potion.as:56]
at runtime::ContentPlayer/simulationSendTouchEvent()
at runtime::SimulatedContentPlayer/clientSocketDataHandler()
The line it's reffering to for the error message (Line 56) has the following:
this.parent.removeChild(this);
My imports:
` import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.*;
import flash.events.TouchEvent;
import flash.net.dns.AAAARecord;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;`
I know the problem is not with the other portions of my code because I have tried switching this line out with visible = false; and I get no errors. Therefore, I am certain that the issue is with the way I am removing the MovieClip, and that is where I need help.
Similiar Sources I have tried that do not work:
How to make a MovieClip remove itself in AS3?

parent is undefined in your example, hence the error "a term is undefined...".
You can avoid the error by wrapping your code in:
if (parent) {
parent.removeChild(this);
}
But based on your comment providing [object global] is sounds like you might actually want something like:
event.currentTarget.parent.removeChild(event.currentTarget);
Which would remove the object that the touch event listener was added to.

Related

Error #1063: Argument count mismatch - AS3

I have a very simple animation i want to make but am stuck with these errors
ArgumentError: Error #1063: Argument count mismatch on ReverseCirculation_Complete_Graham_1_ActionScript3_fla::MainTimeline/Play(). Expected 0, got 1.
The idea is an animation will be paused, then clicked on "play" button to start with the ability to stop or pause the animation before clicking "Next" to preview the continued animation.
My code looks like this:
//imports needed
stop();
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Stage;
Play_btn.addEventListener(MouseEvent.CLICK,Play);
Stop_btn.addEventListener(MouseEvent.CLICK,Stop);
Next_btn.addEventListener(MouseEvent.CLICK,Next);
function Play()
{
blue1_mc.play();
red_mc.play();
green_mc.play();
}
function Stop()
{
blue1_mc.stop();
red_mc.stop();
green_mc.stop();
}
function Next(event:MouseEvent):void
{
gotoAndStop(2);
}
Many thanks
Graham
In AS3, when you define a function like so:
function Play()
You are telling it that there are no parameters/arguments for this function. Unlike similar ECMAScript based languages such as JavaScript, if you end up passing a parameter - for example doing Play("Hello") - it will throw an error.
When you setup a event handler, like you do here:
Play_btn.addEventListener(MouseEvent.CLICK,Play);
That event when triggered calls the function specified - Play - and passes it a MouseEvent object that describes the event (such as the object clicked, the mouse position etc.)
Since you've define the Play function to accept no arguments, you get the error saying there's an unexpected argument. (same for the Stop function).
To remedy the issue, you can do 1 of these two things:
Add a MouseEvent argument to the function:
function Play(event:MouseEvent){
Add a MouseEvent argument, but make it optional:
function Play(event:MouseEvent = null){
The second option, means that you can still simply call Play() without passing it an argument.

Adding the same function to multiple buttons in Actionscript 3.0

I am trying to add the same button function to 2 different symbols in Flash. One is the logo and the other is text that I converted to a symbol that will show itself during the end scene.
I don't understand what I am doing wrong, but I am extremely new to Actionscript & Flash.
My code looks like this:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.net.navigateToURL;
import flash.net.URLRequest;
myButton, txtButton.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
function onClick(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.true.land"), "_blank");
}
But I am getting this error:
Attempting to launch and connect to Player using URL
C:\Users\Angela\Desktop\ASU\GIT 314\Assignment
7\AngelaRogers_Assignment7.swf [SWF] C:\Users\Angela\Desktop\ASU\GIT
314\Assignment 7\AngelaRogers_Assignment7.swf - 351066 bytes after
decompression TypeError: Error #1009: Cannot access a property or
method of a null object reference. at
Button/frame1()[Button::frame1:7]
You must write it twice - once for each button, starting with only it's name: child.addEventListener. There is no shortcut to add the same for two objects at once.
You can write a shortcut function to do this easily enough. (As pointed out by others, your comma is what is causing the error). This I believe is more what you're after: (especially if you keep adding more buttons).
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.net.navigateToURL;
import flash.net.URLRequest;
addClick(myButton, txtButton); //you can add as many items as you want as parameters
function addClick(...buttons):void {
//the ...buttons parameter is known as the '...rest' parameter, and is an array of all the parameters passed to the function
for each(var btn:Sprite in buttons){ //loop through all the items passed in and add the listener
btn.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
}
}
function onClick(e:MouseEvent):void{
navigateToURL(new URLRequest("http://www.true.land"), "_blank");
}

Adobe Flash Pro CS6 movie looping after adding code to actionpanel

i have got a problem and cannot figure out. Everything is fine when i test the scene, i have added stop(); to my movie clip timeline and everything is fine until no code is added to the main stage. As soon as i start adding code to the stage, it starts looping? Because of this i'm stuck with a problem and cannot even develop further, because even mouse click events wont work of that looping... The code i am trying to add is just a simple move layer off stage after click on play button:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
introScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
moveScreenOff(introScreen);
}
function moveScreenOff(screen:MovieClip):void
{
//Move the screen off...
var introTween = new Tween(screen,"x",Strong.easeInOut,screen.x,(screen.width)*-1,1,true);
//When the motion has finished...
introTween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);
function tweenFinish(e:TweenEvent):void
{
trace("tweenFinish");
//Establish the game state...
gameState = STATE_INIT_GAME;
trace(gameState);
//Fire off the gameLoop function at the frame rate of the movie...
addEventListener(Event.ENTER_FRAME, gameLoop);
}
}
The reason your animations are looping continuously is because you have errors in your code.
Access of undefined property gameState.
Access of undefined property STATE_INIT_GAME.
Access of undefined property gameState.
Access of undefined property gameLoop.
You are trying to reference members that haven't been created yet or don't exist. Your main timeline starts at frame 1 with the code you referenced trying to access those variables and the gameLoop method. You need to setup the variables in the first frame to allow them to be referenced. i.e. under your import statements:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var gameState:String;
const STATE_INIT_GAME:String = "GAME_INIT";
then you need to add the gameLoop function:
function gameLoop(e:Event):void {
trace('game loop running');
}
If you don't want those variables and that function setup there then you need to go to the frame using gotoAndStop or gotoAndPlay where those functions and variables can be declared and/or initialized, and stored into memory. Then they will be accessible.

How to implement custom google search for ActionScript 3.0

I was trying to implement custom google search in my course-ware that developed in flash. I define a class named 'Main' (Main.as) and put my search code there. But the problem is, that Main class has a conflict with the other codes containing in my course-ware (i've combo box & other basic navigation in course-ware). i have no idea how to resolve it. is there any way to put that code in timeline layer? help please.. thanks. here is my Main class:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Main extends Sprite
{
public function Main():void
{
searchButton.addEventListener(MouseEvent.MOUSE_UP, google);
addEventListener(KeyboardEvent.KEY_DOWN, google);
searchTerms.addEventListener(MouseEvent.MOUSE_DOWN, selectText);
}
private function google(e:*):void
{
if(e.type == "mouseUp")
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
else if(e.keyCode == Keyboard.ENTER)
{
navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
}
}
private function selectText(e:MouseEvent):void
{
searchTerms.setSelection(0, searchTerms.length);
}
}
}
From what you shared & the messages here, I assume you are trying to add code through the flash IDE while at the same time having a document class called Main, for your application.
There are lots of ways you may get around this.
Assuming you want keep your timeline code unaltered while adding an instance of the Main class :
Add an empty movie clip to the library, say SearchClass.
Go to properties of the movie clip & click export for actionscript.
Set the class to Main. Do make sure where Main.as lies outside with respect to the fla.
Add this movieclip onto the stage, on any frame or layer.
Do remember to clear the document class field.
As a side note, You should also rename the class Main to something meaningful, like SearchClass.
If you wonder about setting the class vs base class,
We use base class only when you wish to extend the features of the class (by adding UI elements for eg).
You could also simply call the class directly from the timeline code as :
var main:Main = new Main();
addChild(main);
Just make sure the Main.as file lies next to the fla... ie make sure the path is available to compiler.

flashfirebug As3 console timer or setInterval not working

i have tried to use a timer in the as3 console from FlashFirebug addon but it doesn't work (have tried several combinations). The timer code was this:
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
var myTimer = new Timer(1000, 2);
myTimer.addEventListener(TIMER, timerHandler);
myTimer.start();
function timerHandler(event:TimerEvent){trace("timerHandler: ");}
//but it gives this error:
Error #1034: Type Coercion failed: cannot convert r1.deval.rt::FunctionDef#50ef781 to Function.
//also have tried with setInterval with this code:
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
import flash.utils.setInterval;
function d(){trace("cc");}
setInterval(d, 2000);
//but console error says:
Error: Call to a possibly undefined method.
//probably need to import something. tried tho import "import flash.utils.setInterval;" but gives this error:"Error: You must import the class before being able to instantiate it. Ex. import flash.display.MovieClip;"
Can you help me on This? timer or setInterval functions?
Thanks,
John
I'm not sure about the error message you're getting, but you do have an error in your code at the point at which you assign the event listener for your timer. You need to access the event name constant from the TimerEvent class like so:
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
var myTimer = new Timer(1000, 2);
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.start();
function timerHandler(event:TimerEvent){trace("timerHandler: ");}
There's a decent explanation here on the advantages of using the ActionScript 3.0 Timer class over setInterval.
Well, looks like there is no way to achieve this. FlashFirebug uses D.eval library and I can't find that it supports timers or intervals right now.
I'm not sure you've sorted it yet but it looks like:
var num:int = setInterval(d, 2000);
and if you import import flash.utils.clearInterval; you can stop it like:
clearInterval(num)
The problem is on this line:
myTimer.addEventListener(TIMER, timerHandler);
It should be:
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
// add this ^^^^^^^^^^^
This is quite old. But answering it to help anyone in the future.
FlashFirebug 4.8.0 which is still on the approval queue (at the time of answer) should enable you to inject an SWF file inside the running SWF.
That would allow you to use full AS3 capabilities when you hit limitations with the console.