查看您引用的游戏,很明显您将需要一些碰撞检测。本关于像素级碰撞检测的教程可能有助于您入门。
使用您当前的设置,我认为提高绘图速度的唯一方法是提高 fla 的帧速率。
您不能将播放器实例作为子类添加到主类中,因为目前它无权访问它。实际上,在你的玩家类中创建多个精灵是非常低效的。一种更有效的方法是直接在播放器实例的图形实例中绘制矩形。
我认为以下内容可能是您尝试做的一个很好的起点。再次,我包含了一些评论,希望能解释我在做什么。
将此设置为您的 fla 中的文档类:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class MainTrink extends MovieClip {
// define a list of players, their color, and controls
// this should make it easier to vary the number of players
private var players:Array = [
{color: 0xff0000, left: Keyboard.A, right: Keyboard.S},
{color: 0x0000ff, left: Keyboard.O, right: Keyboard.P},
{color: 0x00ff00, left: Keyboard.NUMBER_1, right: Keyboard.NUMBER_2},
{color: 0xffff00, left: Keyboard.LEFT, right: Keyboard.RIGHT},
];
public function MainTrink() {
var playerData:Object;
var player:PlayerTrink;
// Create a player instance for each player specified in the players array
for (var i:int = 0; i < players.length; i ++)
{
playerData = players[i];
player = new PlayerTrink();
player.color = playerData.color;
player.init();
this.addChild(player);
// Add a reference to the instance back onto the players
// list so that we can access it easily in other methods
playerData.player = player;
}
// add listeners for keyboard events
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
this.stage.addEventListener(KeyboardEvent.KEY_UP, _onKeyUp);
}
private function _onKeyDown(event:KeyboardEvent):void
{
var playerData:Object;
var player:PlayerTrink;
// Check whether pressed key is a control for one of our players
for (var i:int = 0; i < players.length; i ++)
{
playerData = players[i];
player = PlayerTrink(playerData.player);
if (event.keyCode == playerData.left)
player.leftKeyDown = true;
if (event.keyCode == playerData.right)
player.rightKeyDown = true;
}
}
private function _onKeyUp(event:KeyboardEvent):void
{
var playerData:Object;
var player:PlayerTrink;
for (var i:int = 0; i < players.length; i ++)
{
playerData = players[i];
player = PlayerTrink(playerData.player);
if (event.keyCode == playerData.left)
player.leftKeyDown = false;
if (event.keyCode == playerData.right)
player.rightKeyDown = false;
}
}
}
}
并将此文件包含在与 fla 相同的目录中:
package {
// Need to import other classes referenced within this class
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
// By convention classes are usually named using CamelCase
public class PlayerTrink extends MovieClip {
// Move your properties out of your class constructor
private const TURNSPEED:Number = 0.1;
private var posX:Number = 50;
private var posY:Number = 50;
private var dirX:Number = 1;
private var dirY:Number = 1;
private var dir:Number = 0;
private var _leftKeyDown:Boolean = false;
private var _rightKeyDown:Boolean = false;
private var _color:Number;
public function PlayerTrink() {
// constructor code
}
public function init():void
{
this.addEventListener(Event.ENTER_FRAME, _onEnterFrame);
}
private function _onEnterFrame(e:Event):void {
if(_leftKeyDown) dir -= TURNSPEED;
if(_rightKeyDown) dir += TURNSPEED;
dirX = Math.cos(dir);
dirY = Math.sin(dir);
posX += dirX;
posY += dirY;
drawRect(posX, posY);
}
private function drawRect(x:Number, y:Number):void {
this.graphics.beginFill(_color);
this.graphics.drawRect(x, y, 4, 4);
this.graphics.endFill();
}
public function set leftKeyDown(value:Boolean):void
{
_leftKeyDown = value;
}
public function set rightKeyDown(value:Boolean):void
{
_rightKeyDown = value;
}
public function set color(value:Number):void
{
_color = value;
}
}
}