![]() |
|
||||||||||
|
|||||
|
Регистрация: Jun 2014
Сообщений: 558
|
Доброго времени суток
Вопрос, пытаюсь кораблик двигаться по инерции. Но когда он летит по оси Y вниз, то код "инерции" не срабатывает. ещё вопрос, можно ли чего добавить, что бы получить более близкую к физике инерцию, не шибко усложняя код. Цели добиться идеала нет и по этому код самый простой ship.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey); function fl_MoveInDirectionOfKey(event: Event) { if (start) { if (ship.x > 1597) { ship.x = 3; } if (ship.x < 3) { ship.x = 1597; } if (ship.y > 897) { ship.y = 3; } if (ship.y < 3) { ship.y = 897; } if (rightPressed) { ship.rotation += 8; } if (leftPressed) { ship.rotation -= 8; } if (upPressed) { if (a <= 6) //ускорение { a += 0.2; } radians = ship.rotation * Math.PI / 180; vectorX = Math.cos(radians); vectorY = Math.sin(radians); ship.x = ship.x - speed * vectorX * a; ship.y = ship.y - speed * vectorY * a; Vx = speed * vectorX * a; Vy = speed * vectorY * a; } else { if ((Vx * s > 0.4) || (Vx * s < - 0.4)) { ship.x -= Vx * s; //s - замедление Vx = Vx * s; } if ((Vy * s > 0.4) || (Vx * s < - 0.4)) { ship.y -= Vy * s; Vy = Vy * s; } a = 0.4; } } } всё спасиб, с ошибкой разобрался, в коде переменные попутал Второй вопрос в принципе актуален Последний раз редактировалось neonoviiwolf; 09.07.2014 в 12:55. |
|
|||||
|
Регистрация: Jun 2013
Адрес: Воронеж
Сообщений: 101
|
Вот так можно:
import flash.display.MovieClip; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.events.Event; var ship:MovieClip; var rotation_speed:Number = 0; var motion_speed:Number = 0; var motion_switch:int = 0; var rotation_switch:int = 0; const MAX_ROTATION_SPEED:Number = 3; const MAX_MOTION_SPEED:Number = 5; const ROTATION_ACCELERATION:Number = 0.2; const MOTION_ACCELERATION:Number = 0.3; const RESISTANCE:Number = 0.98; stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownUpHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyDownUpHandler); stage.addEventListener(Event.ENTER_FRAME, update); function update(e:Event):void { rotateShip(); moveShip(); frameLimits(); } function keyDownUpHandler(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.UP: if ( e.type == KeyboardEvent.KEY_DOWN ) motion_switch = 1; else motion_switch = 0; break; case Keyboard.DOWN: if ( e.type == KeyboardEvent.KEY_DOWN ) motion_switch = -1; else motion_switch = 0; break; case Keyboard.LEFT: if ( e.type == KeyboardEvent.KEY_DOWN ) rotation_switch = -1; else rotation_switch = 0; break; case Keyboard.RIGHT: if ( e.type == KeyboardEvent.KEY_DOWN ) rotation_switch = 1; else rotation_switch = 0; break; } } function rotateShip():void { rotation_speed += rotation_switch * ROTATION_ACCELERATION; if ( Math.abs( rotation_speed ) > MAX_ROTATION_SPEED ) rotation_speed = MAX_ROTATION_SPEED * rotation_switch; ship.rotation += rotation_speed; rotation_speed *= RESISTANCE; } function moveShip():void { motion_speed += motion_switch * MOTION_ACCELERATION; if ( Math.abs( motion_speed ) > MAX_MOTION_SPEED ) motion_speed = MAX_MOTION_SPEED * motion_switch; ship.x -= Math.sin( ship.rotation * Math.PI / 180 ) * motion_speed; ship.y += Math.cos( ship.rotation * Math.PI / 180 ) * motion_speed; motion_speed *= RESISTANCE; } function frameLimits():void { if ( ship.x > stage.stageWidth ) ship.x = 0; else if ( ship.x < 0 ) ship.x = stage.stageWidth; if ( ship.y > stage.stageHeight ) ship.y = 0; else if ( ship.y < 0 ) ship.y = stage.stageHeight; }
__________________
В лесу родилась ёлочка, в лесу она росла! Зимой и летом... |
|
|||||
|
Регистрация: Jun 2014
Сообщений: 558
|
Ёлочка шикарно, сегодня вечером попробую, спасибо
меня ещё интересуют заносы |
|
|||||
|
Регистрация: Jun 2013
Адрес: Воронеж
Сообщений: 101
|
Цитата:
import flash.display.MovieClip; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.events.Event; import flash.geom.Point; var ship:MovieClip; var ship_speed_xy:Point = new Point( 0, 0 ); var motion_speed:Number = 0; var motion_speed_xy:Point = new Point( 0, 0 ); var motion_switch:int = 0; var motion_angle:Number = 0; var rotation_speed:Number = 0; var rotation_switch:int = 0; var skid_rotation:Number = 0; const SKID_DELAY:Number = 0.05; const MOTION_ACCELERATION:Number = 1; const MOTION_SPEED_MAX:Number = 10; const MOTION_RESISTANCE:Number = 0.95; const ROTATION_ACCELERATION:Number = 1; const ROTATION_SPEED_MAX:Number = 5; const ROTATION_RESISTANCE:Number = 0.8; stage.addEventListener( Event.ENTER_FRAME, update ); stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownUpHandler ); stage.addEventListener( KeyboardEvent.KEY_UP, keyDownUpHandler ); function update( e:Event ):void { rotateShip(); moveShip(); frameLimits(); } function rotateShip():void { rotation_speed += ROTATION_ACCELERATION * rotation_switch; if ( rotation_speed * rotation_switch > ROTATION_SPEED_MAX ) rotation_speed = ROTATION_SPEED_MAX * rotation_switch; ship.rotation += rotation_speed; if ( rotation_switch == 0 ) rotation_speed *= ROTATION_RESISTANCE; } function moveShip():void { motion_speed += MOTION_ACCELERATION * motion_switch; if ( motion_speed * motion_switch > MOTION_SPEED_MAX ) motion_speed = MOTION_SPEED_MAX * motion_switch; if ( motion_switch != 0 ) skid_rotation = ship.rotation; motion_angle = skid_rotation * Math.PI / 180; motion_speed_xy.x = Math.sin( motion_angle ) * motion_speed; motion_speed_xy.y = -Math.cos( motion_angle ) * motion_speed; ship_speed_xy.x += ( motion_speed_xy.x - ship_speed_xy.x ) * SKID_DELAY; ship_speed_xy.y += ( motion_speed_xy.y - ship_speed_xy.y ) * SKID_DELAY; ship.x += ship_speed_xy.x; ship.y += ship_speed_xy.y; if ( motion_switch == 0 ) motion_speed *= MOTION_RESISTANCE; } function frameLimits():void { if ( ship.x > stage.stageWidth ) ship.x = 0; else if ( ship.x < 0 ) ship.x = stage.stageWidth; if ( ship.y > stage.stageHeight ) ship.y = 0; else if ( ship.y < 0 ) ship.y = stage.stageHeight; } function keyDownUpHandler( e:KeyboardEvent ):void { switch ( e.keyCode ) { case Keyboard.UP: case Keyboard.W: if ( e.type == KeyboardEvent.KEY_DOWN ) motion_switch = 1; else motion_switch = 0; break; case Keyboard.DOWN: case Keyboard.S: if ( e.type == KeyboardEvent.KEY_DOWN ) motion_switch = -1; else motion_switch = 0; break; case Keyboard.LEFT: case Keyboard.A: if ( e.type == KeyboardEvent.KEY_DOWN ) rotation_switch = -1; else rotation_switch = 0; break; case Keyboard.RIGHT: case Keyboard.D: if ( e.type == KeyboardEvent.KEY_DOWN ) rotation_switch = 1; else rotation_switch = 0; break; } }
__________________
В лесу родилась ёлочка, в лесу она росла! Зимой и летом... |
|
|||||
|
Регистрация: Jun 2014
Сообщений: 558
|
Ёлочка спасибо, то что нужно
|
![]() |
![]() |
Часовой пояс GMT +4, время: 19:09. |
|
|
« Предыдущая тема | Следующая тема » |
|
|