// forked from rsakane's ミニテニス /* * ミニテニス * というか、卓球風。 * * マウスでラケットを動かしてボールを打ち返してください。 * ラケットにボールが当たる瞬間、タイミング良く左クリックするとスマッシュが出ます。 * * 7本先取。 */ package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Matrix; import org.libspark.betweenas3.BetweenAS3; public class Main extends Sprite { private var ball:Ball; private var player:Player; private var enemy:Enemy; private var frame:int = 0; public function Main() { var matrix:Matrix = new Matrix(); matrix.createGradientBox(465, 465); graphics.beginGradientFill("linear", [0xdddddd, 0x333333, 0xdddddd], [1.0, 1.0, 1.0], [0, 128, 255], matrix); graphics.drawRect(0, 0, 465, 465); graphics.endFill(); matrix.createGradientBox(465, 465, 90 * Math.PI / 180); graphics.beginGradientFill("linear", [0xdddddd, 0x333333, 0xdddddd], [1.0, 1.0, 1.0], [0, 128, 255], matrix); graphics.moveTo(0, 465); graphics.lineTo(232, 232); graphics.lineTo(465, 465); graphics.moveTo(232, 232); graphics.lineTo(0, 0); graphics.lineTo(465, 0); graphics.endFill(); graphics.beginFill(0x444444, 1.0); graphics.drawRect(232 - 40, 232 - 40, 80, 80); graphics.endFill(); Score.init(this); var shadow:Shadow = new Shadow(); shadow.x = 232; shadow.y = 470; addChild(shadow); enemy = new Enemy(); addChild(enemy); ball = new Ball(shadow); ball.x = ball.y = 232; addChild(ball); player = new Player(); addChild(player); addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { if (Score.endFlag) removeEventListener(Event.ENTER_FRAME, onEnterFrame); ball.move(); player.x = ball.x; player.y = ball.y; if (ball.z <= 90 && ball.vz < 0 && range(player, ball)) { player.rotationX = 30; if (ball.vz < -40) { ball.vz *= -1; ball.vz += 5; } else { ball.vz = 70; ball.GRAVITY = 0.2; } }else{ player.rotationX = 0; } if (ball.z < 0 && ball.vz < 0) { if (range(player, ball)) { ball.vz *= -1; ball.vx = Math.random() * 16 - 8; } else if (ball.z <= -500) { ball.z = -500, ball.vz = 40; Score.blue++, Score.update(); wait(); } } if (ball.z >= 2000) { if (range(enemy, ball)) { ball.vz *= -1; ball.vx = Math.random() * 16 - 8; if (ball.vz > 40 && Math.random() >= 0.5) ball.vz -= 5; } else { Score.red++, Score.update(); ball.z = -500; wait(); } ball.GRAVITY = 0.5; } if (frame % 6 == 0) { BetweenAS3.tween(enemy, { x:ball.x, y:ball.y }, null, 0.16).play(); } frame++; } private function range(a1:*, a2:*):Boolean { var distance:int = Math.sqrt(Math.pow(a1.x - a2.x, 2) + Math.pow(a1.y - a2.y, 2)); if (distance < a1.RADIUS + a2.RADIUS) { return true; } return false; } private function wait():void { ball.vz = 1; BetweenAS3.delay ( BetweenAS3.tween(ball, { vz:40 }, null, 0.1), 1.0 ).play(); } } } import flash.display.Sprite; import flash.filters.BlurFilter; import flash.geom.Matrix; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; class Ball extends Sprite { public var vx:Number = Math.random() * 16 - 8; private var vy:Number = 5; public var vz:int = 40; private var shadow:Shadow; public const RADIUS:int = 20; public var GRAVITY:Number = 0.5; public function Ball(shadow:Shadow) { this.shadow = shadow; this.filters = [new BlurFilter()]; var matrix:Matrix = new Matrix(); matrix.createGradientBox(20, 20, 45 * Math.PI / 180, -10, -10); graphics.beginGradientFill("linear", [0xed1a3d, 0xf791a3], [1.0, 1.0], [0, 255], matrix); graphics.drawCircle(0, 0, RADIUS); graphics.endFill(); } public function move():void { this.z += vz; this.vy += GRAVITY; this.y += vy; this.x += vx; this.rotation++; shadow.z = this.z; shadow.x = this.x; shadow.scaleX = shadow.scaleY = 1 / (465 / this.y); if (this.y >= 465) vy = -15; if (this.y <= 0) this.y = 0, vy += 5; if (this.x <= 0) vx *= -1, this.x = 0; if (this.x >= 465) vx *= -1, this.x = 465; } } class Shadow extends Sprite { public function Shadow() { graphics.beginFill(0x333333); graphics.drawCircle(0, 0, 10); graphics.endFill(); } } class Player extends Sprite { public const RADIUS:int = 50; public function Player() { graphics.beginFill(0xFF0000, 0.3); graphics.drawCircle(0, 0, RADIUS); graphics.endFill(); } } class Enemy extends Sprite { public const RADIUS:int = 50; public function Enemy() { graphics.beginFill(0x009ad6, 1.0); graphics.drawCircle(0, 0, RADIUS); graphics.endFill(); this.z = 2000; } } class Score { public static var red:int = 0; public static var blue:int = 0; public static const MAX:int = 7; public static var tf:TextField; public static var endFlag:Boolean = false; public static function init(canvas:Sprite):void { tf = createTextField("0 - 0", 30); tf.x = 465 - tf.width; canvas.addChild(tf); } public static function update():void { if (red < MAX && blue < MAX) { tf.text = red + " - " + blue; return; } if (red == MAX) tf.text = "RED WIN"; else tf.text = "BLUE WIN"; tf.x -= 55; endFlag = true; } public static function createTextField(text:String, size:int):TextField { var tf:TextField = new TextField(); tf.defaultTextFormat = new TextFormat("_typeWriter", size, 0x0, true); tf.text = text; tf.autoSize = TextFieldAutoSize.LEFT; return tf; } } TASさんがミニテニスをするようです forked from: ミニテニス