Flash AS3 : HitTest unexpected error - actionscript-3

If have a Problem with this bit of code in my Flash AS3 Project:
//Hit Test between Objects
function hitTest(a,b:DisplayObject):Boolean {
if (b.hitTestObject(a) == true) then {
return true;
} else {
return false;
}
}
I am calling it like so:
trace(hitTest(player, c_lvl.gold1));
Player is an Object created in my Code (instance of a movieclip):
player = new Player();
world.addChild(player);
gold1 is an instance of a movieclip packed in another movieclip called c_lvl.
Im getting 2 errors:
/Users/nicolasbrauch/Google Drive/info_1/script.as, Line 141 1008: Attribute is invalid.
/Users/nicolasbrauch/Google Drive/info_1/script.as, Line 143 1083: Syntax error: else is unexpected.
Line 141 is the first Line of my hitTest function.
What shall I do? Where is my error? I dont get it...

As akmozo said, you shouldn't have a then keyword after your if conditional. Once you remove that keyword, you will have fixed both errors.

Related

How to refer to a single frame in a an object class?

So, I have my character movieclip on frame1 of my timeline. When it hits an enemy the movie goes on frame2 where I have a game-over screen. However I get this error at runtime when the character hits the enemy:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Coinsy/update()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/CharMovement()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/ResetBox()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/EnemyBall()
I debugged and it seems that the problem is in my coins class:
package
{
import flash.display.*;
import flash.events.*;
public class Coinsy extends MovieClip
{
var char:MovieClip;
var MTL = MovieClip(root);
public function Coinsy()
{
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
char = MTL.char1;
// CC = int(root).CC;
if(hitTestObject(char))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MTL.CC++;
}
}
}
}
at char = MTL.char1;, apparently I can't use MovieClip(root) when I have more then one frame.
char1 is my character's instance name andCC is the coin count var.
I'm pretty new at ActionScript and I'd like to know where i did wrong. THANKS!
The error is actually happening on this line
if(hitTestObject(char))
What's happening is the game is still trying to check for collisions on the game-over screen. The char1 object couldn't be found, so it became null. Null essentially means something's empty, so you can't do anything with it. If you ever try null.something you'll get an error, and someFunction(null) will quite likely cause an error too.
A quick fix is changing the line to
if(char != null && hitTestObject(char))
as Karma mentioned.
However, the following three TypeErrors are unrelated to the code you posted. They're happening because some line of code is trying to do null.something. The CharMovement, ResetBox and EnemyBall functions are probably being called when they shouldn't be. You might need to remove event listeners when game-over happens, or add extra logic to check for null values.

MovieClip(root) line seems to be crashing my game

Inside my classes, I invoke this function
MovieClip(root).increaseScore();
which handles the score in the main .as file.
It all works fine during the execution of the level. However when the level is finished and the screen goes to another frame, the game crashes and gives me this error
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
on the line above.
How do I fix this?
Thanks
edit:
This is were I tell it to addScore, this is in the GameController.as file
private function removeBubble(bubble, addScore:Boolean)
{
var delay:Timer = new Timer(200, 1);
delay.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent)
{
if(bubble.parent==mcGameStage)
{
var j:int = bubbleList.indexOf(bubble);
bubbleList.splice(j,1);
if(addScore) bubble.addScore();
mcGameUI.txtScorePlayer.text = String(playerScore);
mcGameStage.removeChild(bubble);
}
e.currentTarget.removeEventListener(e.type, arguments.callee);
checkWin();
});
delay.start();
}
here is the checkWin function:
private function checkWin()
{
if (playerBlue + playerRed + playerYellow + playerOrange + playerPurple + playerGreen == 0)
{
gameWin();
}
}
private function gameWin()
{
while (bubbleList.numChildren > 0)
{
bubbleList.removeChildAt(0);
}
mcGameUI.btnMixBlue.removeEventListener(MouseEvent.CLICK, mixBlue);
mcGameUI.btnMixRed.removeEventListener(MouseEvent.CLICK, mixRed);
mcGameUI.btnMixYellow.removeEventListener(MouseEvent.CLICK, mixYellow);
mcGameUI.btnNeedle.removeEventListener(MouseEvent.CLICK, activateNeedle);
mcGameStage.removeEventListener(Event.ENTER_FRAME,update);
mcGameStage.removeEventListener(MouseEvent.CLICK, checkToHit);
removeEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
if (mouseCursor != null)
{
mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);
mouseCursor = null;
}
gotoAndPlay("level1win");
}
And inside my classes,
public function addScore()
{
root["increaseScore"]();
}
This is what increaseScore does
public function increaseScore()
{
playerScore += 1000;
}
So where is the null object? D:
Also I am very inexperienced using the debugger so I apologize if this could be easily solved with that. I tried it and couldn't figure it out before coming here.
What is OPP method?
What is FrameScript?
Also, the class is MovieClip
thanks :)
Fixing this is done with the help of the debugger!
The error is telling you that something is null and you're trying to access it. That's not good, so let's think what could be null?
1) Your movie clip instance might be null.
2) increaseScore() method of your movie clip instance might try to access something that is null
You didn't post any code that I could analyse at the time I'm writing this answer, so I can't say for sure.
Some possible problems:
Your class is not called MovieClip, but you're just trying to cast your root object to a MovieClip. Thing is, MovieClip's don't have a increaseScore() method. You should instead call the increaseScore() method with
root["increaseScore"]();
This will call the method of your root timeline, but since we are using weak-typing, you might have problems with debugging it later. But I guess that's the price you pay when writing all the code in a frame instead of using OPP approach.
The true reason here is said in the error: Cannot access a property or method of a null object reference.
What this means is that MovieClip(root) is null (it doesn't even bother to check if the function is present). The reasons for this might be three:
Your document class is not MovieClip (could be Sprite).
The class you use root in, is not DisplayObject. The property is part of the DislpayObject class and so if you use it on other kind of classes that does not extend it, it won't work.
You are using this piece of code before you have added the instance to the stage (most probable cause). The root property represents the top-most display object in the tree. If you haven't added the child to the tree, it has no roots :) Check out parent - the error should be the same.

Play Movie Clip in reverse and return to frame 1

I'm doing a little animation using Actionscript 3.0 but I'm having a few problems (I'm new with AS3) when I want to play a movie clip in reverse and, then, return to frame 1(main).
Here is my code:
B6_btn.addEventListener(MouseEvent.CLICK, onClickReverse6);
function onClickReverse6(event:MouseEvent):void{
m6_mc.addEventListener(Event.ENTER_FRAME, playReverse6, false, 0, true);
}
function playReverse6(event:Event):void{
if(m6_mc.currentFrame == 1){
if(playMusic){
playMusic.stop();
}
gotoAndStop(1);
}else{
m6_mc.prevFrame();
}
}
The error I get is with the line
"if(m6_mc.currentFrame == 1)" - ERROR #1009: Cannot access a property
or method of a null object reference
If I remove the command gotoAndStop(1), no error is presented.
Can anyone, please, help me with my code?
As far as I can see, you are calling gotoAndStop some other MovieClip (it's actually whatever this is in your case). But at this time you are checking the currentFrame property fo the m6_mc.
I think there might be some error with that. If you switch this to frame 1, is there a m6_mc instance (if you're not keeping it in a variable)?
I have this code on frame 1, regarding the code presented before:
function onClick6(event:MouseEvent):void{
musicLoader = new URLRequest("music/GABRIEL.mp3");
music = new Sound();
music.load(musicLoader);
playMusic = music.play(0,4);
gotoAndStop(7);
m6_mc.gotoAndPlay(1);}

as3 cs5 Odd Error #1009 in drag and drop game

I'm making a drag-and-drop game.
On the MouseEvent.MOUSE_UP I have an if-statement like so:
function fnUp(event:MouseEvent):void
{
clicked.stopDrag();
if (clicked.dropTarget.parent.parent.name == clicked.currentLabel)
{
var dropped:Card = clicked.dropTarget.parent.parent as Card;
dropped.gotoAndStop(dropped.name);
dropped.bg.gotoAndStop("done");
removeChild(clicked);
}
else
{
clicked.x = clicked.sx;
clicked.y = clicked.sy;
clicked.bg.gotoAndStop("off");
}
clicked = null;
}
"clicked" is an earlier declared var of type Card(); and it's set in the MouseEvent.MOUSE_DOWN like so:
function fnDown(event:MouseEvent):void
{
clicked = event.currentTarget as Card;
clicked.bg.gotoAndStop("on");
clicked.startDrag();
addChild(clicked);
}
I thought that if I build my if-statement like this there wouldn't be an error if the card was dropped anywhere but on one of the three possible target cards. But I still get:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
tldr; I get an error if I drop my card outside one of the 3 possible targets.
On which line you getting an error? You need to check if all of the following objects existing:
if (clicked.dropTarget &&
clicked.dropTarget.parent &&
clicked.dropTarget.parent.parent )
{
then do something...
}

Error #1034 while running flash application

Alright, I know that this error is occurring because of something to do with addchild() function. On frame 2. How do I know? Because when I remove the snippets of code that dynamically place an object onto the stage it works fine. The error is below and the source code for frame 2 is below the error messages.
Error Code
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to flash.display.DisplayObject.
at fl.motion::AnimatorBase/play()
at fl.motion::AnimatorBase$/processCurrentFrame()
at fl.motion::AnimatorBase/handleEnterFrame()
at flash.display::MovieClip/nextFrame()
at Lemonade_fla::MainTimeline/begin()[Lemonade_fla.MainTimeline::frame1:27]
TypeError: Error #1034: Type Coercion failed: cannot convert 0 to
FRAME 2 CODE
flash.display.DisplayObject.
stop();
var guide_tut:guide = new guide();
addChild(guide_tut);
//stand.addEventListener(MouseEvent.CLICK, check);
addEventListener(Event.ENTER_FRAME, check);
function check(ev : Event) : void {
cash.text.text = cash1;
lemons_count.text.text = lemons1;
cups_count.text.text = cups;
straws_count.text.text = straws;
ice_count.text.text = ice;
}
shop_mc.addEventListener(MouseEvent.CLICK, shopnow);
function shopnow(event:MouseEvent):void{
nextFrame();
}
1) I ran your code it works fine, the only place the error could be generated is:
flash.display.DisplayObject.
should be: flash.display.DisplayObject;
but you don't need it.
and
var guide_tut:guide = new guide();
addChild(guide_tut);
I ran it with just an empty mc, it works fine.
2) no reason to have (i know its commented out) but there never a reason to have
stand.addEventListener(MouseEvent.CLICK, check) ever since check is EnterFrame
basically you're error is within either display, most likely you're not extending the class from MovieClip
public class guide extends MovieClip
instead of movieclip it can be sprite or other displayobject...