基本上,我正试图让一个随机生成的角色跟随一系列航点到达他需要去的地方,而不是在舞台上走进墙壁等。
我这样做是通过将一个点数从引擎传递给Character的followPath函数(这将是一个循环,但我尚未到达那个阶段)。
此followPath函数的一部分是检测角色何时足够接近航点,然后继续前进到下一个航点。为此,我尝试使用Point.distance(p1,p2)来计算当前所选航点与表示角色当前位置的点之间的距离。
这是我遇到这个问题的地方。试图更新角色的当前(x,y)点位置证明是困难的。出于某种原因,Point.setTo函数似乎不存在,尽管它在文档中。结果,我正在使用
currentPos.x = x;
currentPos.y = y;
//update current position point x and y
尝试这样做,这就是我的1009错误来自的地方。
到目前为止,这是我的完整角色类:
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方法的信息。
答案 0 :(得分:3)
您正在尝试分配不存在的Point对象的x和y属性。
你必须创建你的点:
currentPos = new Point ();
然后分配x和y
答案 1 :(得分:1)
问题是你没有首先使用Point构造函数。 创建不是简单数据类型(Int,Number,String ...)的变量时,必须先调用构造函数,然后才将属性赋值给对象的字段。 这是因为您必须在访问其属性之前初始化Point类的实例。 任何其他类都是如此。
http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29
“在面向对象的编程中,类中的构造函数(有时缩写为ctor)是在创建对象时调用的特殊类型的子例程。它准备新对象以供使用。”
基本上,您没有准备新的Point对象。
在此示例中,在构造函数(公共函数字符)
期间public function Character() {
//add these lines (you can omit the zeroes as the default value is zero)
//I added the zeroes to show that the constructor can set the initial values.
wayPoint = new Point(0, 0);
currentPos = new Point(0, 0);
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;
}
记住每个新对象标识引用NULL(无),直到你构造一个对象或做这样的事情
var pointA = pointB;
//where pointB is already not null
//You can also check this
if(currentPos != null)
{
currentPos.x = X;
currentPos.y = Y;
}
如果首先使用构造函数,currentPos将不为null。
祝你好运。