我正在尝试使用paper.js构建一个小型赛车游戏,我在将汽车对象与我创建的轨道一起移动时遇到了麻烦。
我已经搜索了其他线程,但没有找到bezier弯曲轨道的解释。 我已经画了一个赛道和一辆车。汽车位于赛道上,但它没有移动,虽然我实现了一个onFrame功能,它应该改变它的位置。
我猜测底部的onFrame函数中racingtrack.getPointAt(i*length)
有问题,但我无法弄清楚是什么。
代码对我有意义,racingtrack.getPointAt(offset)
以上就可以了。
这是我的代码:
//DRAW A RACING TRACK
var racingtrack = new Path();
racingtrack.strokeColor='#DA0734';
racingtrack.strokeWidth=4;
var handleInTop = new Point(-90, 0);
var handleOutTop = new Point(90, 0);
var handleInSide = new Point(0, -90);
var handleOutSide = new Point(0, 90);
Segment1 = new Segment(new Point(230, 100), handleOutTop, handleInTop);
Segment2 = new Segment(new Point(100, 275), handleInSide, handleOutSide);
Segment3 = new Segment(new Point(230, 450), handleInTop, handleOutTop);
Segment4 = new Segment(new Point(435, 450), handleInTop, handleOutTop);
Segment5 = new Segment(new Point(765, 100), handleInTop, handleOutTop);
Segment6 = new Segment(new Point(970, 100), handleInTop, handleOutTop);
Segment7 = new Segment(new Point(1100, 275), handleInSide, handleOutSide);
Segment8 = new Segment(new Point(970, 450), handleOutTop, handleInTop);
Segment9 = new Segment(new Point(765, 450), handleOutTop, handleInTop);
Segment10 = new Segment(new Point(435, 100), handleOutTop, handleInTop);
racingtrack.add(Segment1, Segment2, Segment3, Segment4, Segment5, Segment6, Segment7, Segment8, Segment9, Segment10);
racingtrack.closed=true;
racingtrack.fullySelected=false;
racingtrack.position = view.center;
//DRAW A RACING CAR
var racingcar = new Path();
racingcar.fillColor = '#DA0734';
var handleInRightCar = new Point(0, 13)
var handleOutRightCar = new Point(0, -13)
FirstCorner = new Point(100, 470);
SecondCorner = new Point(100, 496);
ThirdCorner = new Point(140, 496);
FourthCorner = new Segment(new Point(145, 483), handleInRightCar, handleOutRightCar);
FifthCorner = new Point(140, 470);
racingcar.add(FirstCorner, SecondCorner, ThirdCorner, FourthCorner, FifthCorner);
racingcar.closed = true;
//PUT RACINGCAR ON STARTING POINT
var offset = 0;
var startposition = racingtrack.getPointAt(offset);
racingcar.position = startposition;
//MOVE CAR ALONGSIDE RACINGTRACK
//set the number of parts the track is divided into
var amount = 100;
//calculate the length of one part
var length = racingtrack.length / amount;
//animate the car, moving from position to position alongside the racingtrack
var carposition = new Point();
function onFrame(event){
for(var i=0; i < amount+1; i++){
carposition = racingtrack.getPointAt(i * length);
racingcar.position = carposition;
}
}
答案 0 :(得分:2)
请记住,onFrame尝试每秒执行60次。
这意味着你正在移动100个位置X 60fps ==每秒6000次绘制== 抽取太多
此代码将每隔1 1/2秒将您的汽车绕轨道发送。
var currentPosition=0;
function onFrame(event){
carposition = racingtrack.getPointAt(currentPosition * length);
racingcar.position = carposition;
if(++currentPosition>100){currentPosition=0;}
}