If "x" collides with "y" gotoAndPlay...

Page 1 of 1

1 Replies - 2379 Views - Last Post: 27 September 2009 - 10:24 AM

#1 ProgrammerZach

Reputation: 3
• Posts: 12
• Joined: 23-July 09

If "x" collides with "y" gotoAndPlay...

Posted 26 September 2009 - 03:52 PM

Alright, so basically I'm making my dad a little game for his birthday, and I am pretty new to Flash CS4/ActionScript 3.

I am trying to make it so that when my main character collides with the chimney of the house, a new level begins. The new level being the inside of the house. My current working code is:

```var vy:Number=0;
var jumped:Boolean=false;
var gv:Number=1;

function ctrl_hero(e:Event) {
if (hero.x-20<mouseX) {
hero.x+=5;
}
if (hero.x+20>mouseX) {
hero.x-=5;
}
vy+=gv;
if (! level.hitTestPoint(hero.x,hero.y,true)) {
hero.y+=vy;
}
if (vy>10) {
vy=10;
}
for (var i:int = 0; i<10; i++) {
if (level.hitTestPoint(hero.x,hero.y,true)) {
hero.y--;
vy=0;
jumped=false;
}
}
}

function jump(e:Event) {
if (! jumped) {
hero.y-=5;
vy=-18;
jumped=true;
}
}

function collide(event:Event) {
if (hero.hitTestObject(knob)) {
errormsg.text="The Door Is Locked, Try a Different Tactic.";
} else {
}
}

```

What I tried was to add a new function that is basically something like this

```addEventListener(Event.ENTER_FRAME,chimney);

function chimney(event:Event) {
if (hero.hitTestObject(chimney)) {
gotoAndPlay(2)
} else {
gotoAndPlay(1)
}
}

```

When I try that I get a duplicate function error, so then I just put the function code with the other function code. So there would be 2 "if" statements, and 2 "else" statements. I am not sure what to do, maybe I am using the wrong code entirely? Any help would be appreciated.

Is This A Good Question/Topic? 0

Replies To: If "x" collides with "y" gotoAndPlay...

#2 ProgrammerZach

Reputation: 3
• Posts: 12
• Joined: 23-July 09

Re: If "x" collides with "y" gotoAndPlay...

Posted 27 September 2009 - 10:24 AM

Alright so I got it to work, and just in case somebody comes here with my issue I will post my solution.

```function collide(event:Event) {
if (hero.hitTestObject(knob)) {
errormsg.text="The Door Is Locked, Try a Different Tactic.";
} else {
}
if (hero.hitTestObject(chimney)) {
gotoAndPlay(2)
}else{
stop();
}

}

}
```

I just added onto the collide function, simple stuff. I also added

```stop();
```

to my main layer to keep it from skipping to my next frame.