-
key functions not working
i wrote this code so that a plane can go right and left across a map of japan.
Code:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.display.Loader;
import flash.net.URLRequest;
public class map extends Sprite {
public function map() {
var plane:Loader = new Loader();
var backmap:Loader = new Loader();
plane.load(new URLRequest("plane2.gif"));
plane.x = 20;
plane.y = 20;
backmap.load(new URLRequest("map.gif"));
backmap.x = 0;
backmap.y = 0;
addChild(plane);
addChild(backmap);
stage.addEventListener(KeyboardEvent.Key_Down, keyPressedDown);
}
private function keyPressedDown(event:KeyboardEvent):void {
var x_speed:int = 2;
switch (key) {
case Keyboard.LEFT :
x_speed = 2;
break;
case Keyboard.RIGHT :
x_speed = -2;
break;
}
plane.x += x_speed;
}
}
}
but when i compile it gives me this
Code:
50 Error: Access of possibly undefined property Key_Down through a reference with static type Class.
stage.addEventListener(KeyboardEvent.Key_Down, keyPressedDown);
^
/home/ryland/Desktop/as3/map.as(34): col: 21 Error: Access of undefined property key.
switch (key) {
^
/home/ryland/Desktop/as3/map.as(42): col: 13 Error: Access of undefined property plane.
plane.x += x_speed;
^
/home/ryland/Desktop/as3/map.as(42): col: 13 Error: Access of undefined property plane.
plane.x += x_speed;
^
-
Senior Member
Do you have all capital Letters for KEY_DOWN?
Also you need to make plane a class variable:
private var plane:Loader;
Currently it is a local variable, but you use it again in another function and then it is undefined.
Last edited by cancerinform; 12-20-2008 at 09:56 AM.
- The right of the People to create Flash movies shall not be infringed. -
-
KEY_DOWN or KeyPressedDown?
Originally Posted by cancerinform
Do you have all capital Letters for KEY_DOWN?
Also you need to make plane a class variable:
private var plane:Loader;
Currently it is a local variable, but you use it again in another function and then it is undefined.
Originally Posted by squigy_dunkens
private function keyPressedDown(event:KeyboardEvent):void {
var x_speed:int = 2;
switch (key) {
case Keyboard.LEFT :
x_speed = 2;
break;
case Keyboard.RIGHT :
x_speed = -2;
break;
}
i put keyPressedDown. is that wrong?
also, should i make backmap a private var too?
Last edited by squigy_dunkens; 12-20-2008 at 11:55 AM.
-
Senior Member
keyPressedDown is only a variable for the listener function. Important is this:
KeyboardEvent.KEY_DOWN
- The right of the People to create Flash movies shall not be infringed. -
-
Code:
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.display.Loader;
import flash.net.URLRequest;
public class map extends Sprite {
public function map() {
private var plane:Loader = new Loader();
private var backmap:Loader = new Loader();
plane.load(new URLRequest("plane2.gif"));
plane.x = 20;
plane.y = 20;
backmap.load(new URLRequest("map.gif"));
backmap.x = 0;
backmap.y = 0;
addChild(plane);
addChild(backmap);
stage.addEventListener(KeyboardEvent.Key_Down, keyPressedDown);
}
private function KEY_DOWN(event:KeyboardEvent):void {
var x_speed:int = 2;
switch (key) {
case Keyboard.LEFT :
x_speed = 2;
break;
case Keyboard.RIGHT :
x_speed = -2;
break;
}
plane.x += x_speed;
}
}
}
would that work then?
Last edited by squigy_dunkens; 12-21-2008 at 01:43 PM.
-
Senior Member
You haven't done any changes, which I suggested. It would give you several errors.
- The right of the People to create Flash movies shall not be infringed. -
-
sorry, im not getting it, could you post what the code should look like?
-
Senior Member
There were some other problems, which I fixed:
PHP Code:
package
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.Loader;
import flash.net.URLRequest;
public class map extends Sprite
{
private var plane:Loader;
public function map ()
{
plane = new Loader ;
var backmap:Loader = new Loader ;
plane.load (new URLRequest("plane2.gif"));
plane.x = 20;
plane.y = 20;
backmap.load (new URLRequest("map.gif"));
backmap.x = 0;
backmap.y = 0;
addChild (plane);
addChild (backmap);
stage.addEventListener (KeyboardEvent.KEY_DOWN,keyPressedDown);
}
private function keyPressedDown (event:KeyboardEvent):void
{
var x_speed:int=2;
switch (event.keyCode)
{
case Keyboard.LEFT :
x_speed = -x_speed;
break;
case Keyboard.RIGHT :
x_speed = x_speed;
break;
}
plane.x += x_speed;
}
}
}
- The right of the People to create Flash movies shall not be infringed. -
-
thanks, it compiled correctly now. but when i tried it all i saw was the map. i suspected that the plane must not be visible because it is behind the map. so i took the map image out of the directory so that the map would not show, and i was correct, i saw the plane. so i assumed to get the plane on top, a have to load it after the map is loaded, and i came up with this code:
PHP Code:
package { import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.display.Loader; import flash.net.URLRequest;
public class map extends Sprite { private var plane:Loader; public function map () { plane = new Loader ; var backmap:Loader = new Loader ; backmap.load (new URLRequest("map.gif")); backmap.x = 0; backmap.y = 0; plane.load (new URLRequest("plane2.gif")); plane.x = 20; plane.y = 20; addChild (plane); addChild (backmap); stage.addEventListener (KeyboardEvent.KEY_DOWN,keyPressedDown); } private function keyPressedDown (event:KeyboardEvent):void { var x_speed:int=2; switch (event.keyCode) { case Keyboard.LEFT : x_speed = -x_speed; break; case Keyboard.RIGHT : x_speed = x_speed; break; } plane.x += x_speed; } } }
after i put the map image back in the directory, i compiled it. but when i tried it, the plane was still in back of the map. how would i fix this?
-
Try changing these two lines
PHP Code:
addChild (plane);
addChild (backmap);
to
PHP Code:
addChild (backmap);
addChild (plane);
Just add the graphics in the order you need them layered. Or, later, use the setChildIndex() method (see onboard help)
-
thanks. now its almost working the way i want it to. but if you look here, you will see there is still quite a few problems.
- everything is positioned wrong
- the plane is supposed to be an animated image, but its not doing anything
- when you drive the plane using the arrow keys, its not very smooth
- basicaly i want it all to look like this java applet i made previously here
-
my main problem is that all the images are too big for some reason, and its all positions like 50 pixels away from where it should be. can anyone help.
-
any help? i just need to know why every thing is so big and wrongly positioned.
-
I looked at your posted swf and compared it to map.gif. I do see that it is positioned oddly, but instead of being too big it is smaller than map.gif appears on its own.
I don't see any reason the map would be positioned wrongly. Is your map the document class? If not, is it positioned/scaled in any way?
Flash does not natively handle animated gifs well, you might want to turn the map into a png instead.
And for the plane, you might want to use
http://code.google.com/p/as3gif/
I did not find any java applet at the link you provided.
-
all i did was put the position for both x and y as 0, but that definately isnt 0 in the swf.
also sorry, the java applet used to be there, heres where it is now:
http://placelandia.com/fake-websites/jptravel/map2.html
-
no answer?
i take the blankness of this forum to mean this is an unexplainable problem, so no one knows the answer.
-
Senior Member
No you did not. The plane Loader is at x=20 and y=20. That contains the plane gif.
- The right of the People to create Flash movies shall not be infringed. -
-
i know that already, but what about the map in the background(backmap)?
-
Senior Member
I just looked at your source code, which is only this part:
<embed src="map.swf" width="460" height="491"/>
You need to publish your movie correctly first and then see what you get. Use the publish button.
- The right of the People to create Flash movies shall not be infringed. -
-
i changed the source to this
Code:
<html>
<head>
<title>map</title>
</head>
<body>
<embed src="map.swf" width="460" height="491"/>
</body>
</html>
nothing happened, is there something wrong with the embed code?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|