00:00
00:00
Newgrounds Background Image Theme

MatthieuxDancingDead just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 frame jumping

1,019 Views | 1 Reply
New Topic Respond to this Topic

AS3 frame jumping 2016-09-20 16:26:54


Basically, i'm wanting to make a "find the items" game. currently i'm just testing it with one object to see how it goes. i'm wanting a variable to be set to true after an object is pressed.

here's the code :

//VARIABLES
var Object1 = false;

function clicked (clickInformation) {

Object1 = true;

}

if (Object1 == true) {

gotoAndPlay(2);
}

//EVENTLISTENERS

O2.addEventListener("click",clicked)

Response to AS3 frame jumping 2016-09-21 09:38:54


While I typically don't recommend using frames for games, if you're very new to programming a state machine can be a bit overwhelming. (Though I still highly recommend reading up on them.)

As for the problem: nothing is happening when you click the button because this section of code:

if (Object1 == true) {

gotoAndPlay(2);
}

is executed once, before your event listener is set. So after you click the button, and the event listener triggers, you're changing the value of Object1 but no further code is executed; that if-statement is only executed once, and when it is executed, the value of Object1 is always false, so nothing happens.

In order for it to change frames like you want it to, you'll need to either execute that if-statement again (for example, but putting it in a function and calling said function) or just put the gotoAndPlay(2) inside the callback for your event listener:

var Object1 = false;

function clicked (clickInformation) {
  gotoAndPlay(2);
}

O2.addEventListener("click", clicked);

Also, what you're writing isn't AS3; it's AS1/AS2. You should look into proper AS3 as it's a more versatile language than the previous versions of ActionScript. You can easily get up and running with AS3 with FlashDevelop, which is a solid Flash IDE, or likely whatever you're using right now.

This is a decent rundown of the differences between AS3 and AS1/AS2.