Интересный пример машинки. Машинкой можно управлять с клавиатуры (вверх, вниз, влево, вправо). Чтобы машинка ехала вперед, нужно нажать клавишу вверх. Для поворотов нужно использовать клавиши влево и вправо. Для езды задом - клавиша вниз.
Код
// см. исходный файл ниже
//movement speed of the car
var speed=7;
//Compute pi/180. The value is used twice for each frame, so storing it as a variable saves CPU
var piOver180=Math.PI/180;
onEnterFrame=function(){
//If the left or right keys are down, rotate the wheels
if(Key.isDown(Key.LEFT)){
car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation-5));
car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation-5));
}
if(Key.isDown(Key.RIGHT)){
car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation+4));
car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation+4));
}
//check if the up or down keys are pressed
if(Key.isDown(Key.UP)||Key.isDown(Key.DOWN)){
//if the up key is down, the car moves forward
if(Key.isDown(Key.UP)){
carDirection=1;
//otherwise move the car backwards
}else{carDirection=-1;}
//default turning to false
turning=0;
//if the left or right keys are pressed, set turning to true and rotate the car
if(Key.isDown(Key.LEFT)){
turning=1;
car._rotation-=5*carDirection;
}
else if(Key.isDown(Key.RIGHT)){
turning=1;
car._rotation+=5*carDirection;
}
//if the car isn't turning, reset the rotation of the tires
if(!turning){
car.leftwheel._rotation=0;
car.rightwheel._rotation=0;
}
//move the car. The y coordinate plane is flipped in flash, so we use - instead of +
car._x+=Math.sin(car._rotation*piOver180)*speed*carDirection;
car._y-=Math.cos(car._rotation*piOver180)*(speed)*carDirection;
}
}
Добавлять комментарии могут только зарегистрированные пользователи сайта.
Если у Вас уже есть учетная запись на Kbyte.Ru, пройдите процедуру авторизации.
Если Вы еще не зарегистрированы на Kbyte.Ru - зарегистрируйтесь.