'Spacebar mechanism'

  • 408 Views
  • 11 Replies
New Topic Respond to this Topic
dbzmf2
dbzmf2
  • Member since: Nov. 27, 2011
  • Offline.
Forum Stats
Member
Level 11
Filmmaker
'Spacebar mechanism' Jun. 27th, 2012 @ 12:32 PM Reply

Does anyone know how the 'Spacebar mechanism' of Super Mario Bros Z works. I can't get it to work.

I got this code, but I can't use it right because the video keeps on playing, while in smbz it keeps on the dialogue until you press 'Spacebar'.

The code:

var myKeyListener:Object = new Object();

Key.addListener(myKeyListener);

myKeyListener.onKeyUp = function ()
{
if (Key.getCode() == Key.SPACE)
{
gotoAndPlay(453);
Key.removeListener(myKeyListener);
}
}

caseymacneil
caseymacneil
  • Member since: Nov. 19, 2008
  • Offline.
Forum Stats
Member
Level 06
Programmer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 09:30 PM Reply

what you're doing is setting it to a frame then removing the listener, im not sure what you're trying to do?

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Supporter
Level 10
Programmer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 09:46 PM Reply

At 6/27/12 09:30 PM, caseymacneil wrote: what you're doing is setting it to a frame then removing the listener, im not sure what you're trying to do?

He wants it so when he presses Space he goes to X frame.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 10:12 PM Reply

At 6/27/12 12:32 PM, dbzmf2 wrote: Does anyone know how the 'Spacebar mechanism' of Super Mario Bros Z works. I can't get it to work.

I got this code, but I can't use it right because the video keeps on playing, while in smbz it keeps on the dialogue until you press 'Spacebar'.

The code:

var myKeyListener:Object = new Object();

Key.addListener(myKeyListener);

myKeyListener.onKeyUp = function ()
{
if (Key.getCode() == Key.SPACE)
{
gotoAndPlay(453);
Key.removeListener(myKeyListener);
}
}

Whoa. Okay.
First: I'm not sure that a Listener is an Object (though I may be wrong)
Second:

stage.addEventListener(KeyboardEvent.KEY_UP, myKeyListener);
private function myKeyListener(e:KeyboardEvent):void{
	if(e.keyCode == 32){
		yourMC.gotoAndPlay(453);
		stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyListener);
	}
}

Third: That's AS3, and I believe you're using AS2

_root.onEnterFrame = function(){
	if(Key.isDown(32)){
		yourMC.gotoAndPlay(453);
	}
}

or

onClipEvent(enterFrame){
	if(Key.isDown(32)){
		gotoAndPlay(453);
	}
}

Fourth: Why in the world does your movieclip have 453 frames? That should not have that many frames. Split it into smaller MCs, you'll be glad you did when you have to go back and edit them.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Supporter
Level 10
Programmer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 10:21 PM Reply

That code's correct, I wrote it myself, the just can't implement it properly, test it yourself:

//On a frame
var myKeyListener:Object = new Object();
Key.addListener(myKeyListener);
myKeyListener.onKeyUp = function()
{
	if (Key.getCode() == Key.SPACE)
	{
		trace("test");
		Key.removeListener(myKeyListener);
	}
};

I see most people don't use AS2 listeners and prefer the AS1 approach. . .


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 11:03 PM Reply

At 6/27/12 10:21 PM, MintPaw wrote: That code's correct, I wrote it myself, the just can't implement it properly, test it yourself:

I haven't tested it yet, but that's odd... Wouldn't getCode() return a number? You're comparing a number to Key.SPACE

I see most people don't use AS2 listeners and prefer the AS1 approach. . .

Yeah, I never really used listeners in AS2. Why? Because I had no idea how. Just old habits resurfacing.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 11:14 PM Reply

double-post, but after a bit of tinkering i've figured some things out.

1) class Key contains a public static "space", which is an int(uint?) of 32
2) Mint's right, the code does in fact, work.
3) Listeners are Objects - interesting.
4) onKeyUp can, in fact, be a function
5) You can add listeners to a pre-built class that's intended for input

and here I thought I was supposed to be learning AS3 .-.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Supporter
Level 10
Programmer
Response to 'Spacebar mechanism' Jun. 27th, 2012 @ 11:53 PM Reply

1) class Key contains a public static "space", which is an int(uint?) of 32

Number. Datatypes int and uint don't exist in AS2.
How did you think Key.isDown(Key.LEFT) worked?

2) Mint's right, the code does in fact, work.

Yes it does.

3) Listeners are Objects - interesting.

As are everything.

4) onKeyUp can, in fact, be a function

What else could if of been.

5) You can add listeners to a pre-built class that's intended for input

Key is an EventDispatcher in AS2. Key.isDown() is a deprecated functions that's no longer fully supported by AS2 but still may be used.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
dbzmf2
dbzmf2
  • Member since: Nov. 27, 2011
  • Offline.
Forum Stats
Member
Level 11
Filmmaker
Response to 'Spacebar mechanism' Jun. 28th, 2012 @ 04:57 AM Reply

At 6/27/12 11:53 PM, MintPaw wrote:
1) class Key contains a public static "space", which is an int(uint?) of 32
Number. Datatypes int and uint don't exist in AS2.
How did you think Key.isDown(Key.LEFT) worked?
2) Mint's right, the code does in fact, work.
Yes it does.
3) Listeners are Objects - interesting.
As are everything.
4) onKeyUp can, in fact, be a function
What else could if of been.
5) You can add listeners to a pre-built class that's intended for input
Key is an EventDispatcher in AS2. Key.isDown() is a deprecated functions that's no longer fully supported by AS2 but still may be used.

Thank you all for you help. It worked but with a different code (sorry, I found it out like 2 minutes before I went to sleep).
This is the code I used:

stop ();
onEnterFrame = function ()
{
if (!Key.isDown(32))
{
_root.allow = true;
}
else if (Key.isDown(32) && _root.allow == true)
{
play ();
_root.allow = false;
}
};

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Supporter
Level 10
Programmer
Response to 'Spacebar mechanism' Jun. 28th, 2012 @ 11:09 AM Reply

Make sure to "delete onEnterFrame" when you don't want Space to advance anymore.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 25
Programmer
Response to 'Spacebar mechanism' Jun. 28th, 2012 @ 11:25 AM Reply

At 6/27/12 11:14 PM, egg82 wrote: 3) Listeners are Objects - interesting.

You need something that has the handler function.
You can omit the "listener" object, by doing Key.addListener(this); and placing the function on the main timeline.

5) You can add listeners to a pre-built class that's intended for input

That's why most of the time a generic Object is created, which is dynamic.

and here I thought I was supposed to be learning AS3 .-.

If you look at AsBroadcaster you will see that As3 is actually not that far away.
It's just not buried as deep into As2 as EventDispatcher is in As3. Which made it a bit uncommon.

RumpNissen
RumpNissen
  • Member since: Feb. 25, 2013
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to 'Spacebar mechanism' Apr. 13th, 2013 @ 09:36 AM Reply

Sorry to bring this up again like so many others.

But I'm looking for a similar function. However I want audio to keep playing and make it possible for animation loops.