1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Main extends Sprite
{
private var right:Boolean = (false);
private var left:Boolean = (false);
private var up:Boolean = (false);
private var down:Boolean = (false);
private var recTanglee:Rectangle = new Rectangle();
private var objett: Player = new Player();
public function Main()
{
objett.x = 400;
objett.y = 300;
addChild(objett);
recTanglee.x = 600;
recTanglee.y = 100;
addChild(recTanglee);
stage.addEventListener(Event.ENTER_FRAME, direction);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kDown);
stage.addEventListener(KeyboardEvent.KEY_UP, kUp);
}
public function direction(e:Event):void
{
if (left)
objett.x -= 10;
if (right)
objett.x += 10;
if (up)
objett.y -= 10;
if (down)
objett.y += 10;
objett.wall();
}
private function kDown(e:KeyboardEvent):void
{
if (e.keyCode == 68)
{
right = true;
}
if (e.keyCode == 81)
{
left = true;
}
if (e.keyCode == 90)
{
up = true;
}
if (e.keyCode == 83)
{
down = true;
}
}
private function kUp(e:KeyboardEvent):void
{
if (e.keyCode == 68)
{
right = false;
}
else if (e.keyCode == 81)
{
left = false;
}
else if (e.keyCode == 90)
{
up = false;
}
if (e.keyCode == 83)
{
down = false;
}
}
}
}
Player.as:
package
{
import flash.display.MovieClip;
public class Player extends MovieClip
{
public var floor:int = 450;
public function Player()
{
this.graphics.beginFill(0xDB97DD);
this.graphics.drawRect(this.x - 20, this.y - 20, 40, 40);
this.graphics.endFill();
}
public function wall():void
{
if (this.x - this.width / 2 < 0)
this.x = this.width/2;
if (this.x + this.width / 2 > 800)
this.x = 800 - this.width / 2;
}
}
}
Rectangle.as:
package
{
import flash.display.MovieClip;
public class Rectangle extends MovieClip
{
public function Rectangle()
{
this.graphics.beginFill(0x000000);
this.graphics.drawRect(this.x, this.y, 50, 400);
this.graphics.endFill();
}
}
} |
Partager