我正在尝试在Flash AS3中制作一个RPG风格的游戏。当他击中树木和建筑物等物体时,我试图让我的角色停下来。有没有办法可以将多个对象链接在一起,并让代码说出像 object1 , object2 和 object3 这样的内容将其命名为 multipleobjects &#39 ;?我已经设置了代码,以便角色停在一棵树上,但我不确定如何/如果你可以组合对象,这样他就不会同时经历多个。非常感谢先进! 对象: manmc(我的角色),treer1_MC(第一棵树)treer2_MC(第二棵树)等等
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
manmc.gotoAndStop ("Stand Front Frame");
var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var manSpeed:Number = 3;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function keyDownHandler (keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = true;
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = true;
}
}
function keyUpHandler (keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
manmc.gotoAndStop("Stand Right Frame");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
manmc.gotoAndStop("Stand Left Frame");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
manmc.gotoAndStop("Stand Back Frame");
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
manmc.gotoAndStop("Stand Front Frame");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed)
{
if(manmc.x < 1050)
{
manmc.x += manSpeed;
}
manmc.gotoAndStop("Walk Right Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("leftHit");
manmc.x -= 3;
}
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("rightHit");
manmc.x += 3;
}
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("downHit");
manmc.y -= 3;
}
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("upHit");
manmc.y += 3;
}
}
}
答案 0 :(得分:0)
使用数组来跟踪多个对象。
然后使用循环迭代数组中作为单个对象的所有元素,而不是操纵单个对象。