我怀念我的第一个 AS2 游戏。正如我将解释的那样,最好的方法可能是面向对象的方法。
在 AS2 中,有一个自动内置到对象中的 hittest 方法。这里有一个很好的关于 Kirupa 的教程:
http://www.kirupa.com/developer/actionscript/hittest.htm
还有
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001314.html
首先,您需要使用 Box 类生成您的盒子。您的课程需要如下所示:
//Box.as pseudo-code
class Box {
var x_pos:Number;
var y_pos:Number;
var attachedToPlayer:Boolean;
function Box(_x:Number, _y:Number) {
this.x_pos = _x;
this.y_pos = _y;
}
//other code here
}
请参阅本教程,了解如何将类附加到库中的对象:
http://www.articlesbase.com/videos/5min/86620312
要创建一个新盒子,你可以使用类似的东西
盒子1 =新盒子(100,200);
// 在位置 100x,200y 处创建一个框
但是,您还需要将要拾取的块存储到某种数组中,以便循环遍历它们。见http://www.tech-recipes.com/rx/1383/flash-actionscript-create-an-array-of-objects-from-a-unique-class/
例子:
//在你的主方法顶部附近的某个地方,或者你的主游戏循环从哪里运行 - 注意 Box.as 需要在同一个文件夹中
导入框;
//...then, somewhere before your game loop
//create an array to hold the objects
var boxArray:Array = new Array();
//create loop with i as the counter
for (var i=0; i<4; i++)
{
var _x:Number = 100 + i;
var _y:Number = 100 + i;
//create Box object
var box:Box = new Box();
//assign text to the first variable.
//push the object into the array
boxArray.push(box);
}
同样,您需要为您的玩家创建一个类,并在游戏开始时创建一个新的 Player 对象,例如
var player = new Player(0,0);
然后,您可以针对主游戏循环(即更新玩家位置和其他游戏属性的循环)数组中的块为您的玩家运行一个 hittest 方法。可能有更有效的方法可以做到这一点,例如只循环当前在屏幕上的块。
创建数组后,使用 foreach 循环在游戏的主循环中对玩家运行命中测试,例如
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
}
这基本上是“对于我们输入数组的每个盒子,检查玩家是否正在触摸盒子。如果盒子正在触摸,则使用盒子作为播放器类中方法的参数(我随意称为attachObjectMethod)”。
在 attachObjectMethod 中,您可以定义某种行为来将盒子附加到播放器上。例如,您可以为 box 类中的框的 x 和 y 位置创建一个 get 和 set 方法,以及一个名为 attachToPlayer 之类的有用的布尔值。调用 attachObjectMethod 时,它将设置框的布尔值,例如在 Player 类中
//include Box.as at the top of the file
import Box;
//other methods, e.g. constructor
//somewhere is the Player.as class/file
public function attachObjectMethod (box:Box) {
box.setattachedToPlayer(true);
//you could also update fields on the player, but for now this is all we need
}
现在,玩家碰撞过的盒子的 attachToPlayer 布尔值为真。回到我们的游戏循环,然后我们将修改我们的循环以更新盒子的位置:
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
box.updatePosition(player.get_Xpos, player.get_Ypos);
}
在我们的 Box 类中,我们现在需要定义“updatePosition”:
//Box.as pseudo-code
class Box {
var x_pos:Number;
var y_pos:Number;
var attachedToPlayer:Boolean;
function Box(box_x:Number, box_y:Number) {
this.x_pos = box_x;
this.y_pos = box_y;
}
public function updatePosition(_x:Number, _y:Number) {
if (this.attachedToPlayer) {
this.x_pos = _x;
this.y_pos = _y;
}
}
//other code here
}
如您所见,我们可以传递玩家的位置,如果已设置 attachToPlayer 布尔值,则更新框的位置。最后,我们给盒子添加一个move方法:
public function move() {
if (this.attachedToPlayer) {
this._x = x_pos;
this._y = y_pos;
}
}
更新位置示例:
http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-smoothly-slide-objects-around-in-Flash/ID-17/
最后,我们需要在游戏循环中调用 move 方法:
//assuming you have an array called 'boxArray' and player object called 'player'
for(var box in boxArray){
if (player.hittest(box)) {
player.attachObjectMethod(box);
}
box.updatePosition(player.get_Xpos, player.get_Ypos);
box.move();
}
你还指定了只有在上面没有任何东西的情况下,方块才应该随着玩家移动。当您调用您的 attachToPlayer 方法时,您还需要在盒子和可能位于盒子顶部的对象之间运行一个 foreach 循环。您现在应该从上面的代码中对如何做到这一点有了一个大致的了解。
我很欣赏这是一个相当冗长的答案,而且我还没有机会测试所有代码(事实上我很肯定我在某个地方犯了一个错误) - 不要犹豫提出问题。我的另一个建议是彻底理解这些概念,然后一次一点地编写自己的代码。
祝你好运!