Error #2007: Parameter hitTestObject must be non-null - actionscript-3

this is my code- it's working and moving to frame 3 but everything is stuck there and I
get this Error #2007
function createMC(event:Event):void
{
var hasa_mc:MovieClip= new hasa();
stage.addChild(hasa_mc);
var halfMc:int=hasa_mc.width/2;
hasa_mc.x=randomNum(70+halfMc,480-halfMc);
hasa_mc.addEventListener(Event.ENTER_FRAME, abc);
hasa_mc.addEventListener(Event.ENTER_FRAME, dropCheckHit);
function dropCheckHit(event:Event):void
{
if (hasa_mc.hitTestObject(hauta1_mc)) {
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
event.target.parent.removeChild(event.target);
countertime++;
score_txt.text=String(countertime*10)
if (countertime==10)
{
gotoAndStop(3);
}

The error code and description refers to the line:
if (hasa_mc.hitTestObject(hauta1_mc)) {
My guess would be the movieclip hauta1_mc does not exist on frame 3 of your movie, so once you go to frame 3 and your dropCheckHit function executes the null reference error is thrown.
To resolve you can remove the enter frame listener and stop checking if the movieclip has been hit:
if (countertime==10)
{
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
gotoAndStop(3);
}
Note: You may need to remove the other listener you have on hasa_mc as well if hasa_mc does not exist in frame 3.

thank you. from where i need to remove also ? still does not works.
when it goes to frame 3 there is new MC that hit new object
function dropCheckHit(event:Event):void {
if (hasa_mc.hitTestObject(hauta1_mc)) {
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
event.target.parent.removeChild(event.target);
countertime++;
score_txt.text=String(countertime*10)
if (countertime==10)
{
gotoAndStop(3);
hasa_mc.removeEventListener(Event.ENTER_FRAME, dropCheckHit);
}
hauta1_mc.nextFrame();}
else if (hasa_mc.y > 380)
{
xdirection = 0;
ydirection = 0;
hasa_mc.x = 190;
hasa_mc.y = 200;
hauta1_mc.x=220;
lifeCounter--; //
life_txt.text=String(lifeCounter);
trace(lifeCounter);
if (lifeCounter==0)
{
gotoAndStop(5);
}
}
}
}

Related

AS3 stop function then add back to menu button

Hello im new in AS3 and i have this simple game project but i cant make back to menu button . can someone help me ?
the project have 3 frames , frame 1=title frame2= menu , frame 3 = game
and i want to go back to frame 1 when the game hit gameover .
but when i go back to frame 1 with GotoandStop , it always show error message like :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at next_fla::MainTimeline/BintangGo()[next_fla.MainTimeline::frame3:88]
or is there a way to reset everything and play again at frame 1 ?
this is my code at frame 3
import flash.events.Event;
game_over.visible=false;
men.visible=false;
var scoree:uint;
tong.addEventListener(Event.ENTER_FRAME,meteorGo)
function meteorGo(ev:Event){
tong.y+=15
tong.rotation+=7
if(tong.y>=810){
tong.y=0-Math.random()*800
tong.x=Math.random()*480
}
if(tong.hitTestObject(wong)){
game_over.visible=true;
men.visible=true;
wong.x=1600-Math.random()*400
}
}
import flash.events.Event;
apel.addEventListener(Event.ENTER_FRAME,apelgo)
apel.y=0-Math.random()*800
apel.x=Math.random()*480
function apelgo(ev:Event){
apel.y+=11
if(apel.y>=810){
apel.y=0-Math.random()*800
apel.x=Math.random()*480
}
if(apel.hitTestObject(wong)){
updateScore5();
apel.y=0-Math.random()*800
apel.x=Math.random()*480
}
}
botol.addEventListener(Event.ENTER_FRAME,botolgo)
botol.y=0-Math.random()*800
botol.x=Math.random()*480
function botolgo(ev:Event){
botol.y+=12
if(botol.y>=810){
botol.y=0-Math.random()*800
botol.x=Math.random()*480
}
if(botol.hitTestObject(wong)){
updateScore2();
botol.y=0-Math.random()*800
botol.x=Math.random()*480
}
}
bintang.addEventListener(Event.ENTER_FRAME,BintangGo)
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
function BintangGo(ev:Event){
bintang.y+=9
if(bintang.y>=810){
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
}
if(bintang.hitTestObject(wong)){
updateScore();
bintang.y=0-Math.random()*800
bintang.x=Math.random()*480
}
}
function init():void
{
scoree = 0;
scorecounter.text = scoree.toString();
}
function updateScore5():void
{
scoree += 5;
scorecounter.text = scoree.toString();
}
function updateScore2():void
{
scoree += 2;
scorecounter.text = scoree.toString();
}
function updateScore():void
{
scoree += 1;
scorecounter.text = scoree.toString();
}
bintanga.addEventListener(Event.ENTER_FRAME,BintangGoa)
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
function BintangGoa(ev:Event){
bintanga.y+=9
if(bintanga.y>=810){
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
}
if(bintanga.hitTestObject(wong)){
updateScore();
bintanga.y=0-Math.random()*800
bintanga.x=Math.random()*480
}
}
bintangb.addEventListener(Event.ENTER_FRAME,BintangGob)
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
function BintangGob(ev:Event){
bintangb.y+=2
if(bintangb.y>=810){
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
}
if(bintangb.hitTestObject(wong)){
updateScore();
bintangb.y=0-Math.random()*800
bintangb.x=Math.random()*480
}
}
bintangc.addEventListener(Event.ENTER_FRAME,BintangGoc)
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
function BintangGoc(ev:Event){
bintangc.y+=9
if(bintangc.y>=810){
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
}
if(bintangc.hitTestObject(wong)){
updateScore();
bintangc.y=0-Math.random()*800
bintangc.x=Math.random()*480
}
}
bintangd.addEventListener(Event.ENTER_FRAME,BintangGod)
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
function BintangGod(ev:Event){
bintang.y+=9
if(bintangd.y>=810){
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
}
if(bintangd.hitTestObject(wong)){
updateScore();
bintangd.y=0-Math.random()*800
bintangd.x=Math.random()*480
}
}
mobil.addEventListener(Event.ENTER_FRAME,MobilGO)
mobil.y=-1600-Math.random()*800
mobil.x=Math.random()*480
function MobilGO(ev:Event){
mobil.y+=20
if(mobil.y>=810){
mobil.y=-1600-Math.random()*800
mobil.x=Math.random()*480
}
if(mobil.hitTestObject(wong)){
game_over.visible=true;
men.visible=true;
wong.x=1600-Math.random()*400
}
}
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
men.addEventListener(TouchEvent.TOUCH_TAP, m12);
function m12(event:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,MobilGO);
removeEventListener(Event.ENTER_FRAME,meteorGo);
removeEventListener(Event.ENTER_FRAME,apelgo);
removeEventListener(Event.ENTER_FRAME,botolgo);
removeEventListener(Event.ENTER_FRAME,BintangGoa);
removeEventListener(Event.ENTER_FRAME,BintangGob);
removeEventListener(Event.ENTER_FRAME,BintangGoc);
removeEventListener(Event.ENTER_FRAME,BintangGod);
removeEventListener(Event.ENTER_FRAME,BintangGo);
removeChild (tong);
removeChild (botol);
removeChild (apel);
removeChild (bintang);
removeChild (wong);
removeChild (bintanga);
removeChild (bintangb);
removeChild (bintangc);
removeChild (bintangd);
removeChild (mobil);
gotoAndStop(1);
}
stop();
tyvm if u can help me
On both frame 1 and frame 2 of your main timeline, try adding stop(); as the very first thing.
Judging by the error message mentioning "frame 3" I am guessing it is trying to go to frame 3, and so explicitly calling stop() as soon as possible might prevent that.

Dragging movie clips in Action Script 3

Hi so recently I have been attempting to Drag a movie clip in AS3 but I'm having some trouble picking up with the hit tests anyone got any ideas? Just to clarify, the issue is that when the movieclips hit the drag test object, they're not executing the gotoframe() function.
initDrag() adds action listeners:
MOUSE_DOWN on the object
MOUSE_UP on the stage so it doesn’t matter if you are off the object
endDrag() removes the action listeners; call this (for each object) before you go to another frame
startADrag()create a rectangle within which the object can be dragged (in this case the stage)
call startDrag() on the object
stopADrag() call stopDrag() on the object from currentObject (but only if currentObject is not null).
var currentObject:MovieClip = null;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
}
}
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
thanks for any advice or answers.
The following code should work as expected. The problem is, as i already stated in my comment, that your calls to hitTestObject(obj) only get executed once, at the very beginning of your application. What you need to do though is to check it constantly.
Think about it, if your calls to hitTestObject-calls only get executed once at the beginning, when you didn't even have a chance to drag one of your objects, it will always return false, right? Because your objects are still in their initial position (outside of the dragtest objecti must assume).
With an event listener for Event.ENTER_FRAME you check it once per frame instead. So even if all the results for hitTestObject are false, it will check them all again on the next frame (if you are currently dragging, controlled through a simple boolean called dragging).
var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
addEventListener(Event.ENTER_FRAME, checkForHit);
function checkForHit(e:Event):void{
if(dragging){
if(block1.hitTestObject(dragtest)){
gotoAndStop("lose");
}
if(block2.hitTestObject(dragtest)){
gotoAndStop(27);
}
if(block3.hitTestObject( dragtest)){
gotoAndStop("lose");
}
if(block4.hitTestObject( dragtest)){
gotoAndStop("lose");
}
}
}
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
dragging = true;
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
currentObject.stopDrag();
dragging = false;
}
}

flash as3 hitdetection function triggering other function

I am trying to code a script in which a movieclip drops a rope and catches fishes that follows it up if it touches it. here is the issue , i am using hitTestObject to detect collision . Ofcourse the problem is that i trigger the function when it touches but as soon as it doesnt touch the function for moving the movie starts so basically the fish goes up for few seconds and then starts moving straight again .
To try and fix that i tried to make a boolean variable which changes to true or false according to hit and accordingly makes the movieclip moves but also doesnt work because as soon as one mc is not touching the other it changes from true to false or 1 to 0 ..tried both (as in with boolean variable and Number variable) . Any help or putting me on the right direction would be highly appreciated . Thank you so much
// fish capture code
this.addEventListener(Event.ENTER_FRAME,handleCollision);
function handleCollision(e:Event):void
{
if (ropeHit.hitTestObject(fishy_1_a))
{
stopFish1();
trace(movefish1);
}
else
{
moveFish1();
}
}
//code enemy fishy
//fish 1 A
function moveFish1()
{
if (fishy_1_a.x < 800)
{
fishy_1_a.x += xSpeed;
}
else if (fishy_1_a.x >= 800)
{
fishy_1_a.x = -100;
}
}
function stopFish1()
{
fishy_1_a.y -= xSpeed;
}
Define some flag, that you can test:
function handleCollision(e:Event):void {
//Check if fishy is caught
if (!fishy_1_a.catched && ropeHit.hitTestObject(fishy_1_a)) {
//Change flag
fishy_1_a.catched = true;
trace("Gotcha!");
}
if (fishy_1_a.catched) {
stopFish1();
}else {
moveFish1();
}
}

else statement always run even the answer is true

tnx! it works. but when i tried to add another object named, p1_2 and add the "trace" thing to the code,
it goes back to the same problem.
p1_1.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
question.text = "shape?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss);
function onClickss(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "circle")
{
p1_1.visible = false;
}
else
{
gotoAndStop(6);
}
}
p1_2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(e:MouseEvent):void
{
question.text = "Color?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss2);
function onClickss2(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "red")
{
p1_2.visible = false;
}
else
{
gotoAndStop(6);
}
}
what should i do.? do i nid to seperate p1_2 in another frame
and make another inputtextfield for it?
im planning to add 5 objects on the stage. until p1_5. -_-
Before the line...
if(ans.text == "circle") {
...add:
trace("ans.text = "+ans.text);
Clicking the submit button will trace the actual value of ans.text to the output panel when you test your movie in Flash. If you get a null reference error then ans has not been instantiated. Otherwise, knowing the actual value of ans.text should point you in the right direction to solve the problem.

Why isn't the child removed from the frame?

I'm trying to remove a movieclip with removeChild() function. My code is below, but it doesn't work.
addEventListener(Event.ENTER_FRAME, lemons_collide);
function lemons_collide(ev : Event) : void
{
if(currentFrame==1)
{
if(cup2.hitTestObject(lemons))
{
lemons.stopDrag();
lemons.x = 35;
lemons.y = -150;
lemons.gotoAndPlay(1);
if(lemons.currentFrame>=14){
removeChild(lemons);
}
}
}
};
The "currentFrame" check is run directly after the "gotoAndPlay".. so the currentFrame is always "1". You will have to run a separate event listener tracking "ENTER_FRAME" on that object, then have that remove the child once it is on Frame 14.