我正在开发一个有两个控件的八哥游戏。一种是用户的左手拇指触摸屏幕左侧的任意位置,这会在 y 轴上上下拖动角色。我在实现时遇到问题的另一个控件是屏幕右下角的一个按钮,它可以让角色发射子弹。
我的问题是如何设置它而不让角色在按下按钮时跳到屏幕底部。
我需要弄乱多点触控才能让它运行吗?
我有一个更具体的问题是如何为八哥中的精灵定义一个矩形?由于starling 没有绘图API,我不能这样做......
touchLayer.graphics.beginFill(0x000000, 0);
touchLayer.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
touchLayer.graphics.endFill();
目前我的角色拖动正在工作,但触摸在屏幕上无处不在(我不知道如何让它只是屏幕的左侧......)
任何建议将不胜感激,谢谢。
这是我要求的 InGame 类的完整代码。
package screens
{
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import objects.Enemy;
import objects.GameBackground;
import objects.Laser;
import objects.Scarlet;
import objects.SnowBall;
import starling.display.Button;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.utils.RectangleUtil;
import starling.utils.deg2rad;
public class InGame extends Sprite
{
private var startBtn:Button;
private var fireBtn:Button;
private var bg:GameBackground;
private var scarlet:Scarlet;
private var enemies:Array;
private var lasers:Array;
private var scarletLocation:Point;
private var lasersLayer:Sprite;
private var enemiesLayer:Sprite;
private var touchLayer:Sprite;
private var enemySpawnDelay:Number;
private var enemySpawnCounter:Number;
private var difficulty:Number;
private var difficultyRate:Number;
private var timePrevious:Number;
private var timeCurrent:Number;
private var elapsed:Number;
private var gameState:String;
private var playerSpeed:Number;
private const MIN_SPEED:Number = 650;
//private var scoreDistance:int;
private var gameArea:Rectangle;
private var touchArea:Rectangle;
private var shape:starling.display.Sprite = new starling.display.Sprite();
private var target:Point = new Point(100, 100);
private var touch:Touch;
private var touchX:Number;
private var touchY:Number;
public function InGame()
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
drawGame();
}
private function drawGame():void
{
bg = new GameBackground();
this.addChild(bg);
scarlet = new Scarlet;
scarlet.x = stage.stageWidth/2;
scarlet.y = stage.stageHeight/2;
this.addChild(scarlet);
startBtn = new Button(Assets.getTexture("PlayBtn"));
startBtn.x = stage.stageWidth * 0.5 - startBtn.width * 0.5;
startBtn.y = stage.stageHeight * 0.5 - startBtn.height * 0.5 + 35;
this.addChild(startBtn);
fireBtn = new Button(Assets.getTexture("FireBtn"));
fireBtn.x = 675;
fireBtn.y = 435;
this.addChild(fireBtn);
//defines area scarlet can fly in
gameArea = new Rectangle(0, 15, stage.stageWidth, stage.stageHeight - 150);
}
public function disposeTemporarily():void
{
this.visible = false;
}
public function initialize():void
{
this.visible = true;
this.addEventListener(Event.ENTER_FRAME, checkElapsed);
scarlet.x = -stage.stageWidth;
scarlet.y = stage.stageHeight * 0.5;
gameState = "idle";
playerSpeed = 0;
difficultyRate = 0.3;
difficulty = 1;
enemySpawnDelay = enemySpawnCounter = 100;
enemies = new Array();
lasers = new Array();
scarletLocation = new Point(200, 400);
addEventListener(Event.ENTER_FRAME, update);
lasersLayer = new Sprite();
enemiesLayer = new Sprite();
touchLayer = new Sprite();
addChild(lasersLayer);
addChild(enemiesLayer);
addChild(touchLayer);
addEventListener(Event.ADDED_TO_STAGE, setupTouchLayer);
touchLayer.addEventListener(Event.TRIGGERED, shootLaser);
//scoreDistance = 0;
startBtn.addEventListener(Event.TRIGGERED, onStartBtnClick);
this.addEventListener(TouchEvent.TOUCH, onTouch);
}
private function onStartBtnClick(event:Event):void
{
startBtn.visible = false;
startBtn.removeEventListener(Event.TRIGGERED, onStartBtnClick);
launchScarlet();
}
private function launchScarlet():void
{
this.addEventListener(TouchEvent.TOUCH, onTouch);
this.addEventListener(Event.ENTER_FRAME, onGameTick);
}
private function onTouch(event:TouchEvent):void
{
touch = event.getTouch(stage);
touchX = touch.globalX;
touchY = touch.globalY;
target.x = event.touches[0].globalX;
target.y = event.touches[0].globalY;
}
private function onGameTick(event:Event):void
{
switch(gameState)
{
case"idle":
//Take off
if (scarlet.x < stage.stageWidth * 0.5 * 0.5)
{
scarlet.x += ((stage.stageWidth * 0.5 * 0.5 + 10) - scarlet.x) * 0.5;
scarlet.y = stage.stageHeight * 0.5;
playerSpeed += (MIN_SPEED - playerSpeed) * 0.05;
}
else
{
gameState = "flying";
}
break;
case"flying":
playerSpeed -= (playerSpeed - MIN_SPEED) * 0.01;
scarlet.y -= (scarlet.y - touchY) * 0.1;
if (-(scarlet.y - touchY) < 150 && -(scarlet.y - touchY) > -150)
{
scarlet.rotation = deg2rad(-(scarlet.y - touchY) * 0.075);
}
if (scarlet.y > gameArea.bottom - scarlet.height * 0.5)
{
scarlet.y = gameArea.bottom - scarlet.height * 0.5;
scarlet.rotation = deg2rad(0);
}
if (scarlet.y < gameArea.top + scarlet.height * 0.5)
{
scarlet.y = gameArea.top + scarlet.height * 0.5;
scarlet.rotation = deg2rad(0);
}
//scoreDistance += (playerSpeed * elapsed) * 0.1;
//trace(scoreDistance);
break;
case"over":
break;
}
}
//addapted from "Shoot"
private function setupTouchLayer(event:Event):void
{
touchLayer.graphics.beginFill(0x000000, 0);
touchLayer.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
touchLayer.graphics.endFill();
touchArea = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
}
private function shootLaser(event:TouchEvent):void
{
makeLaser(scarletLocation);
}
private function makeLaser(scarletLocation:Point):void
{
var newLaser:Laser = new Laser();
newLaser.x = scarletLocation.x;
newLaser.y = scarletLocation.y;
newLaser.xVel = 5;
newLaser.yVel = 5;
newLaser.addEventListener(Laser.PURGE_EVENT, purgeLaserHandler);
lasersLayer.addChild(newLaser);
lasers.push(newLaser);
}
private function purgeLaserHandler(event:Event):void
{
var targetLaser:Laser = Laser(event.target);
purgeLaser(targetLaser);
}
private function purgeLaser(targetLaser:Laser):void
{
targetLaser.removeEventListener(Laser.PURGE_EVENT, purgeLaserHandler);
try
{
var i:int;
for (i = 0; i < lasers.length; i++)
{
if (lasers[i].name == targetLaser.name)
{
lasers.splice(i, 1);
lasersLayer.removeChild(targetLaser);
i = lasers.length;
}
}
}
catch(e:Error)
{
trace("Failed to delete laser!", e);
}
}
private function makeEnemies():void
{
enemySpawnCounter++;
if (enemySpawnCounter > enemySpawnDelay)
{
enemySpawnCounter = 0;
enemySpawnDelay -= difficultyRate;
difficulty += difficultyRate;
makeEnemy();
}
}
private function makeEnemy():void
{
var i:int;
for (i = 0; i < Math.floor(difficulty); i++)
{
var newEnemy:Enemy = new SnowBall();
newEnemy.x = 925;
newEnemy.y = Math.random() * 375 + 50;
//trace(newEnemy.x); trace(newEnemy.y);
newEnemy.xVel = (-Math.random() * difficulty) - 5;
newEnemy.sinMeter = Math.random() * 10;
newEnemy.bobValue = Math.random() * difficulty;
newEnemy.addEventListener(Enemy.PURGE_EVENT, purgeEnemyHandler);
enemiesLayer.addChild(newEnemy);
enemies.push(newEnemy);
}
}
private function purgeEnemyHandler(event:Event):void
{
var targetEnemy:Enemy = Enemy(event.target);
purgeEnemy(targetEnemy);
}
private function purgeEnemy(targetEnemy:Enemy):void
{
targetEnemy.removeEventListener(Enemy.PURGE_EVENT, purgeLaserHandler);
try
{
var i:int;
for (i = 0; i < enemies.length; i++)
{
if (enemies[i].name == targetEnemy.name)
{
enemies.splice(i, 1);
enemiesLayer.removeChild(targetEnemy);
i = enemies.length;
}
}
}
catch(e:Error)
{
trace("Failed to delete enemy!", e);
}
}
private function newHitTest(laser:Laser):void
{
for each (var enemy:Enemy in enemies)
{
if (enemy.status != "Dead" && enemy.hitTest(new Point(laser.x, laser.y)))
{
enemy.destroy();
purgeLaser(laser);
}
}
}
private function checkElapsed(event:Event):void
{
timePrevious = timeCurrent;
timeCurrent = getTimer();
elapsed = (timeCurrent - timePrevious) * 0.001;
}
private function update():void
{
//trace(enemies.length, lasers.length);
for each (var enemy:Enemy in enemies)
{
enemy.update();
}
for each (var laser:Laser in lasers)
{
laser.update();
newHitTest(laser);
}
makeEnemies();
}
//addapted from "Shoot"
}
}