我正试图制作一个“Achtung!die kurve” - 就像在Actionscript 3.0中的游戏一样。
到目前为止,我已经完成了其中一个'蠕虫'的移动,并且工作正常。
我想把它分成不同的类文件,但我似乎无法让它工作。 我有一个.fla文件和4个.as文件。到目前为止,我只有其中两个代码。当我将所有代码直接放入fla文件中时,它的工作完全正常,但是当我将它放入我的播放器类时,它不会“将孩子”添加到舞台上。
这是我的播放器.as文件:http://pastebin.com/hZSxT9cu
我评论addChild的原因是因为我想在mainFile.as中这样做
这是来自主文件:http://pastebin.com/TAwUYrTU 我不太确定如何在主文件中调用它,所以我猜这将是问题。
所有文件都被命名并正确放置在同一个文件夹中,并且flash说我的每个.as文件都是以.fla文件为目标,所以这似乎不是问题。
非常感谢任何帮助。
此外,如果有人知道如何改变我的精灵的速度,那将是非常棒的!似乎无法让这个工作。
祝你好运 的Jesper
答案 0 :(得分:1)
看看你引用的游戏,很明显你需要进行一些碰撞检测。有关pixel-level collision detection的本教程可能对您有所帮助。
使用当前设置,我可以看到提高绘图速度的唯一方法是增加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;
}
}
}