Jump to content


Photo

Game-question


  • Please log in to reply
3 replies to this topic

#1 Pattt

Pattt

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 19 November 2004 - 09:41 AM

I have a button on frame 1, and that button has a code that says if i hit key-left, it gonna jump to frame, how can i do so the go back to frame 1 whem i stop pressing the key?

I've test this code:
---------------------------
on (keyDown "<Left>") {
gotoAndPlay(9);
}

on (keyUp "<Left>") {
gotoAndPlay(1);
}
----------------------------
But then this message came up:
------------------------------
Symbol=player_mc, Layer=Layer 9, Frame=1: Line 4: Invalid mouse event specified.
on (keyUp "<Left>") {

Symbol=player_mc, Layer=Layer 9, Frame=1: Line 5: Statement must appear within on handler
gotoAndPlay(1);

Symbol=player_mc, Layer=Layer 9, Frame=1: Line 6: Unexpected '}' encountered
}

----------------------------------
What is wrong?

#2 nalinda

nalinda

    Young Padawan

  • Members
  • Pip
  • 6 posts
  • Location:Sri Lanka
  • Interests:Flash, dereamviewr, maya, creating flash games

Posted 02 December 2004 - 02:57 AM

:rubber: Try addListner :o

Key.addListener
Key.addListener (listener_name)



Where listener_name is the Key object to be watched for key presses and releases; listener_ name has two methods called onKeyDown and onKeyUp. If listener_name is already registered, the Key.addListener command is ignored.



Compatible with Flash Player 6 and later. There are no known issues with any versions of Flash that support this method.



When a Key object has been registered using the Key.addListener method it responds to all key presses and key releases. Any number of objects can be registered. Whenever a key press or release is detected, all registered Key objects have their onKeyDown or onKeyUp methods invoked.

Description
Key objects have onKeyDown and onKeyUp methods associated with them. For these methods to be invoked the Key object has to be registered using the Key.addListener method. After registering, all key presses and releases are relayed to the registered objects and the onKeyUp and onKeyDown methods, respectively, are triggered. (See also Key.onKeyDown and Key.onKeyUp.)





Code




Additional explanation


Notes



Key.addListener(myKeyobject)


Registers myKeyobject so the onKeyDown and onKeyUp methods are invoked on key activity


If myKeyobject is already registered nothing happens


Examples and practical uses
Key.addListener is used to register a Key object to detect keyboard activities. After a Key object has been created the Key.addListener method is used with that Key object’s name to register the object. A Key object’s onKeyDown and onKeyUp methods must be defined as part of the instantiation of the object, like this:



myKeyobject = new Object();



myKeyobject.onKeyDown = () {

// code for key presses

}

myKeyobject.onKeyUp = () {

// code for key releases

}



After the methods have been defined, the Key object is registered so that the onKeyDown and onKeyUp methods are invoked properly:



Key.addListener (myKeyobject);

Tips and precautions
Although there are no limits to the number of Key objects that can respond to key activity after being registered using Key.addListener, registering too many objects can cause slow response from Flash. Each registered Key object should have their onKeyDown and onKeyUp methods defined.



Note that the Key object does not work well with multiple and simultaneous key presses. For example, if two keys are pressed and held down, when the key press release event occurs you cannot tell using the onKeyDown/onKeyUp events alone which key is still down. The only way you can make this check directly is by using Key.isDown to test for specific keys.



Also, note that on early versions of the Flash player, the movie clip methods movieclip.onKeyUp and movieclip.onKeyDown do not work properly (even when the SWF has browser focus). The onKeyDown and onKeyup methods of the key object do work, and provide the only universally workable way of detecting key presses via an event driven route.

#3 Faken

Faken

    Pimpmaster G

  • Admin
  • 5,966 posts
  • Gender:Male
  • Location:Montreal, Canada

Posted 02 December 2004 - 02:59 AM

Good call Nalinda... a more advanced method than what I would have suggested, but great explanation.

Faken

#4 nalinda

nalinda

    Young Padawan

  • Members
  • Pip
  • 6 posts
  • Location:Sri Lanka
  • Interests:Flash, dereamviewr, maya, creating flash games

Posted 03 December 2004 - 12:04 AM

//paste this code, it should b work, if prob mail me
//nutsynalinda@yahoo.com

leftCheck = "no";

var myListener:Object = new Object();
myListener.onKeyDown = function () {
if((Key.isDown(Key.UP)) && (leftCheck == "no")) {
//trace ("You pressed Left key.");
gotoAndPlay(9);
leftCheck = "ok";

}
}
myListener.onKeyUp = function () {
if((Key.isDown(Key.UP) == false) && (leftCheck == "ok")){
//trace ("You released Left key.");
gotoAndPlay(2);
leftCheck = "no";
}
}
Key.addListener(myListener);




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users