基本上,我试图让一个随机生成的角色跟随一系列航路点到达他需要去的地方,而不会在舞台上走进墙壁等。
我通过将一个点数组从引擎传递给角色的 followPath 函数来做到这一点(这将是一个循环,但我还没有到那个阶段)。
这个 followPath 函数的一部分是检测角色何时足够接近航点,然后移动到下一个。为此,我尝试使用 Point.distance(p1,p2) 来计算当前选择的航点与代表角色当前位置的点之间的距离。
这就是我遇到这个问题的地方。尝试更新角色的当前 (x,y) 点位置被证明是困难的。出于某种原因,Point.setTo 函数似乎不存在,尽管它在文档中。结果,我正在使用
currentPos.x = x;
currentPos.y = y;
//update current position point x and y
尝试这样做,这就是我的 1009 错误的来源。
到目前为止,这是我的完整 Character 课程:
package {
import flash.display.MovieClip;
import flash.geom.Point;
public class Character extends MovieClip {
public var charType:String;
private var charList:Array = ["oldguy","cloudhead","tvhead","fatguy","speakerhead"];
private var numChars:int = charList.length;
private var wpIndex:int = 0;
private var waypoint:Point;
private var currentPos:Point;
private var wpDist:Number;
private var moveSpeed:Number = 5;
//frame labels we will need: charType+["_walkingfront", "_walkingside", "_walkingback", "_touchon", "_touchonreaction", "_sitting"/"_idle", "_reaction1", "_reaction2", "_reaction3", "_reaction4"]
public function Character() {
trace("new character:");
charType = charList[Math.floor(Math.random()*(numChars))];
//chooses a random character type based on a random number from 0-'numchars'
trace("char type: " + charType);
gotoAndStop(charType+"_walkingfront");
x = 600;
y = 240;
}
public function followPath(path:Array):void {
if(wpIndex > path.length){ //if the path has been finished
gotoAndStop(charType+"_sitting"); //sit down
return;//quit
}
waypoint = path[wpIndex];
//choose the selected waypoint
currentPos.x = x;
currentPos.y = y;
//update current position point x and y
wpDist = Point.distance(currentPos,waypoint);
//calculate distance
if(wpDist < 3){ //if the character is close enough to the waypoint
wpIndex++; //go to the next waypoint
return; //stop for now
}
moveTo(waypoint);
}
public function moveTo(wp:Point):void {
if(wp.x > currentPos.x){
currentPos.x += moveSpeed;
} else if(wp.x < currentPos.x){
currentPos.x -= moveSpeed;
}
if(wp.y > currentPos.y){
currentPos.y += moveSpeed;
} else if(wp.y < currentPos.y){
currentPos.y -= moveSpeed;
}
}
}
}
谁能向我解释为什么会这样?这是我现阶段无法克服的障碍。
我也很好奇是否有人可以提供有关为什么我不能使用幻像 Point.setTo 方法的信息。