Forked from: 084's forked from: forked from: forked from: forked from.. diff:56 forked from: forked from: forked from: forked from: forked from: forked from: forked from: ボール 084 forked:2favorite:0lines:131license : MIT License modified : 2011-11-11 22:22:36 Embed Tweet // forked from 084's forked from: forked from: forked from: forked from: forked from: forked from: ボール // forked from 084's forked from: forked from: forked from: forked from: forked from: ボール // forked from 084's forked from: forked from: forked from: forked from: ボール // forked from 084's forked from: forked from: forked from: ボール // forked from 084's forked from: forked from: ボール // forked from 084's forked from: ボール // forked from 084's ボール package { import flash.display.Sprite; import flash.events.Event; import flash.ui.Keyboard; import flash.events.KeyboardEvent; public class Test extends Sprite { private var ball:Ball; private var vx:Number; private var vy:Number; private var ax:Number; private var ay:Number; private var limit:Number; public function Test():void { vx = 0; vy = 0; ax = 0; ay = 0; limit = 20; ball = new Ball(); ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight / 2; addChild(ball); addEventListener(Event.ENTER_FRAME, onEnterFrame); stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp ); } private function onEnterFrame(e:Event):void { var hanbunW:uint = ball.width/2; var hanbunH:uint = ball.height/2; vx += ax; if (vx > limit) { vx = limit; } if (vx < -limit) { vx = -limit; } vy += ay; if (vy > limit) { vy = limit; } if (vy < -limit) { vy = -limit; } ball.x += vx; ball.y += vy; if(ball.x + hanbunW > stage.stageWidth) { vx = 0; vy = 0; ball.x = stage.stageWidth - hanbunW; } else if(ball.x - hanbunW < 0) { vx = 0; vy = 0; ball.x = 0 + hanbunW; } if(ball.y - hanbunH < 0) { vx = 0; vy = 0; ball.y = 0 + hanbunW; } else if(ball.y + hanbunH > stage.stageHeight) { vx = 0; vy = 0; ball.y = stage.stageHeight - hanbunH; } } private function onKeyDown(e:KeyboardEvent):void { if (e.keyCode == Keyboard.LEFT) { ax = -0.2; } else if (e.keyCode == Keyboard.RIGHT) { ax = 0.2; } if (e.keyCode == Keyboard.UP) { ay = -0.2; } else if (e.keyCode == Keyboard.DOWN) { ay = 0.2; } } private function onKeyUp(e:KeyboardEvent):void { if (e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.LEFT) { ax = 0; vx = 0; } else if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { ay = 0; vy = 0; } } } } import flash.display.Sprite; class Ball extends Sprite { private var hankei:Number; private var iro:uint; public function Ball(hanikei:Number = 10, iro:uint = 0x666666) { this.hankei = hanikei; this.iro = iro; init(); } private function init():void { graphics.beginFill(iro); graphics.drawCircle(0, 0, hankei); graphics.endFill(); } } Code Fullscreen Preview Fullscreen keyCode Keyboard.DOWN Keyboard.UP Keyboard.RIGHT Keyboard.LEFT KeyboardEvent KeyboardEvent.ENTER_FRAME KeyboardEvent.KEY_UP KeyboardEvent.KEY_DOWN height width addEventListener addChild Sprite uint Number sort new page view favorite forked pv443 forked from: forked from: fork.. 084 forked:1 favorite:0lines:141 (diff:17) pv35 forked from: forked from: fork.. 084 forked:0 favorite:0lines:131 (diff:1)