error access of undefined event - actionscript-3

i cant seem to go back to the first frame after the game over screen.
i get the error message Scene 1, 1119: Access of possibly undefined property Event through a reference with static type Class in frame 2 is it because i don't have a classset up in frame 1.
import flash.events.Event;
stop();
var isRight:Boolean=false
var isLeft:Boolean=false
var isUp:Boolean=false
var isDown:Boolean=false
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent)
{
if(event.keyCode==39)
{
isRight=true
}
if(event.keyCode==37)
{
isLeft=true
}
if(event.keyCode==38)
{
isUp=true
}
if(event.keyCode==40)
{
isDown=true
}
}
ect...and the next frame code to go back to the previous one is
import flash.events.Event;
restart.addEventListener(Mouse.Event.MOUSE_UP,click)
function click(e:MouseEvent)
{
gotoAndStop(1);
}

you accidentally wrote Mouse.Event.MOUSE_UP instead of MouseEvent.MOUSE_UP in your addEventListener function

Related

How to remove a tween before moving to next frame in as3

I have a movieclip which spins. I want it when users drag and drop it to stop spinning and be in it's initial position. I wrote this code but i get error TypeError: Error #1009: Cannot access a property or method of a null object reference.
at omoixes10_fla::MainTimeline/EntFrame() when I move to next frame. I can not see what i did wrong. Can you please help me with my code? Do I have to remove tween before I move to next frame?
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
tick1.parent.removeChild(tick1);
wrong1.parent.removeChild(wrong1);
sentences2.buttonMode=true;
sentences1.buttonMode=true;
Piece1_mc.buttonMode=true;
var my_x:int=stage.stageWidth
var my_y:int=stage.stageHeight
var myWidth:int=0-my_x;
var myHeight:int=0-my_y;
var boundArea:Rectangle=new Rectangle(my_x, my_y, myWidth ,myHeight);
var spin:Tween=new Tween(Piece1_mc, "rotation",Elastic.easeInOut,0,360,5,true);
spin.stop();
sentences2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
snoopy.gotoAndPlay(2);
addChild(tick1);
addChild(wrong1);
sentences2.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
sentences1.removeEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
spin.start();
spin.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
e.target.yoyo();
{
Piece1_mc.addEventListener(MouseEvent.MOUSE_DOWN, DragP1);
function DragP1 (event:MouseEvent):void
{
Piece1_mc.startDrag();
Piece1_mc.startDrag(false,boundArea);
spin.stop();
}
stage.addEventListener(MouseEvent.MOUSE_UP, DropP1);
function DropP1(event:MouseEvent):void
{
Piece1_mc.stopDrag();
}
if(Targ1_mc.hitTestObject(Piece1_mc.Tar1_mc)) {
Piece1_mc.x=677;
Piece1_mc.y=48,10;
myTimer.start();
spin.stop();
}
}
}
sentences1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
snoopy.gotoAndPlay(64);
}
var myTimer:Timer = new Timer(2000,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void {
gotoAndStop(15);
if (Piece1_mc.parent)
{
Piece1_mc.parent.removeChild(Piece1_mc);
}if (tick1.parent)
{
tick1.parent.removeChild(tick1);
}
if (wrong1.parent)
{
wrong1.parent.removeChild(wrong1);
}
}
}
So the first problem was adding your event listeners inside an enter frame which was not the right way to do since it will keep on adding event listeners at every frame.
Second, you should use a mouse move event listener as I've recommended before to track and test your hit test.
Thirdly, since you are rotating your MovieClip, and you want it to go back to its initial state, you should do :
Piece1_mc.rotation=0;
Hope this helps.

Actionscript3 clearInterval is not working on TouchEvent.TOUCH_BEGIN

I have called a setInterval on TouchEvent.TOUCH_END and I want to clear it when ever screen is touched.
Here is my code:
import fl.motion.MotionEvent;
import flash.display.MovieClip;
import flash.utils.*;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
function onTouchBegin(evt:TouchEvent)
{
clearInterval(MovieClip(root).myInterval);
}
function onTouchEnd(evt:TouchEvent)
{
MovieClip(root).myInterval = setInterval(showTimer,1000);
}
function showTimer()
{
trace("interval working");
}
Your provided code doesn't really explain everything so there will be a fair bit of speculation in this answer:
Possiblity No. 1:
Your way of casting root to MovieClip is not working. Try changing to following:
function onTouchBegin(evt:TouchEvent)
{
var intervalRef:int = (root as MovieClip).myInterval;
clearInterval(intervalRef);
}
Possiblity No. 2:
It seems that your code is written in a frame. And although if you define something in a previous frame it should work, but if you have the code on a different (lower) layer on the same keyframe it could define the variable later and your MovieClip(root).myInterval variable is undefined or null is the value is not defined. So check if your variable even exists:
function onTouchBegin(evt:TouchEvent)
{
trace(MovieClip(root) == null); // see if the root is defined
trace(MovieClip(root).myInterval); // see if the myInterval is defined
}
Possiblity No. 3:
You're cycling through frames. When I had 2 frames: one blank and one with your code, the code didn't work properly. Tested in Flash CC.
Possibility No. 4:
You have a run-time error somewhere else and your whole frame's code is not being executed. Do you use the debug version of Flash Player?
Possible solution for less headache:
Use a Timer. It's easy to control, easy to manage and dispose of. I've edited your code to work with Timer and tested. Feel free to use it for your project.
import fl.motion.MotionEvent;
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
var timer:Timer = (timer == null) ? new Timer(1000) : timer;
timer.addEventListener(TimerEvent.TIMER, onTick);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onTouchBegin); // you can change back to TouchEvent, but since you're using TOUCH_POINT it's pointless so I'd just use MouseEvent
stage.addEventListener(MouseEvent.MOUSE_UP, onTouchEnd);
function onTouchBegin(evt:Event)
{
timer.stop();
}
function onTouchEnd(evt:Event)
{
timer.start();
}
function showTimer()
{
trace("interval working");
}
function onTick(e:TimerEvent):void
{
showTimer();
}
I do want to note that your code works if I just copy-paste it in a single frame Flash file. But still, it's encouraged by Adobe to use Timer instead.

How to delay when my actionscript 3 code runs?

I am creating a drag and drop game that I followed through a Lynda tut. I kept getting an error for my game that I created because I noticed (after weeks of reviewing the code and having other people look at it to figure out what was wrong) that the tutorial that I followed did everything on frame one but I was making my game start at frame 3. So if I start my game at frame 1, it works perfectly and I wont get these errors:
This occurs when I test the movie, once I click continue I am able to see the movie -
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at simpleSpring()[simpleSpring.as:21]
And this occurs when I drag my object -
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at DragDrop/drop()[DragDrop.as:33]
Since I know these errors won't appear unless I begin the game at frame 1, I want to know what code I can place so that I can begin the game at frames that are past the first frame.
The following is the code for DragDrop.as
package
{
import flash.display.*;
import flash.events.*;
public class DragDrop extends Sprite
{
var origX:Number;
var origY:Number;
var target:DisplayObject;
public function DragDrop()
{
// constructor code
origX = x;
origY = y;
addEventListener(MouseEvent.MOUSE_DOWN, drag);
buttonMode = true;
}
function drag(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
startDrag();
parent.addChild(this);
}
function drop(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
stopDrag();
if(hitTestObject(target))
{
visible = false;
target.alpha = 1;
Object(parent).match();
}
x = origX;
y = origY;
}
}
}
And here is the simpleSpring.as
package
{
import flash.display.*;
import flash.events.*;
public class simpleSpring extends MovieClip
{
var dragdrops:Array;
public function simpleSpring()
{
// constructor code
dragdrops = [ladyone,ladytwo,ladythree,ladyfour,ladyfive,ladysix];
var currentObject:DragDrop;
for(var i:uint = 0; i < dragdrops.length; i++)
{
currentObject = dragdrops[i];
currentObject.target = getChildByName(currentObject.name + "_target");
}
}
public function match():void
{
}
}
}
I tried adding the code to a actions layer in the game document but that also does not seem to work correctly.
I am such a newbie when it comes to publishing things. I noticed I can add multiple swf files when I publish for android. This solves my issue of not being able to code at the beginning frame. If I have this game in a separate flash file saved in the same folder with the title movie calling to it with actionscript, the code will work and I am able to publish the whole thing as one file. Thanks again for those that tried to help me figure this out!

ActionScript 3 Flash error 1009 and 2025 combo box

Im trying to insert a music using combo box from the component asset. yesterday works fine, but today suddenly it stopped working and it gave me this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
everytime I clicked on the combo box these error appears, strangely it works fine yesterday.
this is the code for the first frame:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.SoundTransform;
import flash.events.MouseEvent;
//Declare MUSIC Variables
var musicSelection:Sound;
var musicChannel:SoundChannel;
var musicTrack:String = "music/dearly_beloved.mp3";
var musicVolume:Number=0.2;
/*var isMuted = false;*/
loadMusic();
function loadMusic():void
{
//first stop old sound playing
SoundMixer.stopAll();
musicSelection = new Sound();
musicChannel = new SoundChannel();
//create and load the required soun
musicSelection.load(new URLRequest(musicTrack));
//when loaded - play it
musicSelection.addEventListener(Event.COMPLETE, musicLoaded);
}
function musicLoaded(evt:Event):void
{
//finished with this listener
musicSelection.removeEventListener(Event.COMPLETE, musicLoaded);
//play music
playMusic();
}
function playMusic():void
{
//play the random or selected music
musicChannel = musicSelection.play();
//setting the volume control property to the music channel
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
//but add this one to make repeats
musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}
function playAgain(evt:Event):void {
// remove this listener and repeat
musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
playMusic();
}
and this is the code for the second frame:
import flash.events.Event;
menuBtn.addEventListener(MouseEvent.CLICK, goToMenu)
function goToMenu(evt:Event):void
{
gotoAndPlay(2);
}
// BUTTON EVENT LISTENERS
musicCombo.addEventListener(Event.CHANGE, updateMusic);
volumeSL.addEventListener(Event.CHANGE, setSlider);
//process COMBO BOX changes
function updateMusic(evt:Event):void
{
if (musicCombo.selectedItem.data == "none")
{
//no music is required so stop sound playing
SoundMixer.stopAll();
}
else
{
//otherwise load in the selected music
musicTrack = "music/" + musicCombo.selectedItem.data;
loadMusic();
}
}
function setSlider(evt:Event):void
{
//identify the button clicked
var mySlider:Object = (evt.target);
//adjusting to volume of the music channel
musicVolume = mySlider.value;
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
}
the error occurs whenever I clicked on the combo box and when I tried to insert another combo box without putting any data, the same error occurs. hope you guys can help me ASAP cause this is due on friday this week xD
thanks
Alright, found the answer. It seems I need to put these 3 lines of code:
import fl.events.ComponentEvent;
import fl.controls.ComboBox;
import fl.data.DataProvider;

ArgumentError: Error #2025 when removeChild

I wrote a AS3 class to make a Counter function. Click and hold mouse in blue area to define a value. I try to show a indicator picture to users that reminding the variation.
But when I drag mouse a little quick in the blue area a error will occur:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at test/get2()
I have read some similar issue posts, but I can't fix this. Could you give me any help? Thank you!
Download .fla and .as in CS6
Download .fla and .as in CS5
The code is below:
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import flash.events.Event;
public class test extends Sprite{
var i:int=20; //Set a var for the number displayed in stage.
var mx1:Number; //Set a var to save MouseY when MOUSE_DOWN
var mx2:Number; //Set a var to save MouseY when MOUSE_UP
var num:int=0; //Set a var to calculate result
var sub1:subbar1=new subbar1();
var sub2:subbar2=new subbar2();
var sub3:subbar3=new subbar3();
var add1:addbar1=new addbar1();
var add2:addbar2=new addbar2();
var add3:addbar3=new addbar3();
public function test() {
init1(); //Set TextField and addEventListener
initbar(); //Set indicator picture position when drag mouse
}
private function init1():void{
label=new TextField();
label.text=String(i);
label.width=280;
label.selectable=false;
label.x=140;
label.y=90;
addChild(label);
Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1); //addEventListener to bluearea
}
private function initbar(){
sub1.x=sub2.x=sub3.x=add1.x=add2.x=add3.x=30;
sub1.y=35;
sub2.y=55;
add3.y=sub3.y=75;
add2.y=95;
add1.y=115;
}
private function get1(evt:MouseEvent):void{
mx1=mouseY;
trace(mx1);
Controler.removeEventListener(MouseEvent.MOUSE_DOWN,get1);
stage.addEventListener(MouseEvent.MOUSE_UP,get2); //addEventListener to MOUSE_UP
stage.addEventListener(Event.ENTER_FRAME,lifebar); //add ENTER_FRAME to display indicator picture when move mouse
}
private function get2(evt:MouseEvent):void{
mx2=mouseY;
trace(mx2);
if(mx2<=135&&mx2>=35&&mouseX<=130&&mouseX>=50){ //Limited enable area as the blue area
if(num>=4){ //Set i value depends on num
i=i-3;
}else if(num<=-4){
i=i+3;
}else{
i=i-num;
}
label.text=String(i);
}
if(num==1){ //remove indicator picture when MOUSE_UP
removeChild(sub1);
}
if(num==2){
removeChild(sub1);
removeChild(sub2);
}
if(num>=3){
removeChild(sub1);
removeChild(sub2);
removeChild(sub3);
}
if(num==-1){
removeChild(add1);
}
if(num==-2){
removeChild(add1);
removeChild(add2);
}
if(num<=-3){
removeChild(add1);
removeChild(add2);
removeChild(add3);
}
stage.removeEventListener(MouseEvent.MOUSE_UP,get2);
Controler.addEventListener(MouseEvent.MOUSE_DOWN,get1);
stage.removeEventListener(Event.ENTER_FRAME,lifebar);
}
private function lifebar(evt:Event):void{ //Set a ENTER_FRAME to display indicator picture
num=(mouseY-mx1)/12+1;
trace(num);
if(mouseY!=mx1&&num==1){
addChild(sub1);
}
if(num==2){
addChild(sub2);
}
if(num==3){
addChild(sub3);
}
if(num==-1){
addChild(add1);
}
if(num==-2){
addChild(add2);
}
if(num==-3){
addChild(add3);
}
}
}
}
You are attempting to remove display objects that have not yet been added as a child to the display list.
Even though sub1 has been instantiated, it has not been added at the time you attempt to remove it:
Before calling removeChild(obj) on the display object, first test if it has been added as a child by evaluating whether if(contains(obj)) is true.
At line 67 in test.as, you should perform condition testing to see if sub1 has been added to the display list:
if(num==1) {
if(contains(sub1)) // test to see if sub1 is on the display list
removeChild(sub1);
}
If this issue continues with other children, you should add additional testing in blocks like these:
if(num==2){
removeChild(sub1);
removeChild(sub2);
}