[code] Turn Towards & Follow Mouse

thought i’d share this… just trying a quick conversion from AS3 to Lua

http://dl.dropbox.com/u/14822311/Turn_and_Follow.zip

[lua]–[[
* Player Movement - Follow Mouse on click with Easing and smooth rotation
* ---------------------
* VERSION: 1.1
* DATE: 10/01/2010
* AS3
* UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
–]]

–[[

* LUA/CORONA CONVERSION
* AUTHOR: jmp909
* DATE: 11/11/2010

* CREDITS: Spaceship from Rozebau @ http://www.pixeljoint.com/pixelart/48537.htm

]]

– player
local _player

– game play area
local world = display.newGroup()

– other vars
local _vx = .1;
local _vy = .1;
local _currentSpeed = 0;
local _maxSpeed = .3;
local _decay = .98;

–local _maxSpeed = 2.3;
–local _decay = 0.99;

local _targetX = 100;
local _targetY = 100;

local _mouseDown = false;
local _mousePoint = {x=0,y=0}

local stage = display.getCurrentStage()

– CONSTRUCTOR
function init()

createPlayer();

– add listeners
Runtime:addEventListener(“enterFrame”, enterFrameHandler);
Runtime:addEventListener(“touch”, onMouseHandler);

end

– HANDLE TOUCH/DRAG EVENTS
function onMouseHandler(e)

if(e.phase==“began” or e.phase==“moved”) then
_mouseDown = true;
_mousePoint = { x = e.x, y = e.y}
elseif(e.phase==“ended” or e.phase==“cancelled”) then
_mouseDown = false;
_mousePoint = nil
end

end

– CREATE SPACESHIP
function createPlayer()

_player = display.newImage(“spaceship.png”)
_player.x = stage.stageWidth / 2;
_player.y = stage.stageHeight / 2;
world:insert(_player);

end

– UPDATE LOOP
function enterFrameHandler(event)

if (_mouseDown) then

_targetX = _mousePoint.x
_targetY = _mousePoint.y

_vx = _vx + (_targetX - _player.x) / 1000;
_vy = _vy + (_targetY - _player.y) / 1000;

_currentSpeed = math.sqrt(_vx * _vx + _vy * _vy);

if (_currentSpeed < _maxSpeed) then

_vx = _maxSpeed * _vx / _currentSpeed;
_vy = _maxSpeed * _vy / _currentSpeed;
end

end

_vx = _vx * _decay;
_vy = _vy * _decay;

_player.x = _player.x + _vx;
_player.y = _player.y + _vy;

_player.rotation = 90 + math.atan2(_vy, _vx) / math.pi * 180;

end
– START GAME –
init();[/lua] [import]uid: 6645 topic_id: 3556 reply_id: 303556[/import]

this next :wink:
http://www.freeactionscript.com/2008/12/game-weapons-heat-seeking-missiles-rockets-torpedoes/

[import]uid: 6645 topic_id: 3556 reply_id: 10730[/import]

jmp909, would mind to share the lua code for “game-weapons-heat-seeking-missiles” ? [import]uid: 33180 topic_id: 3556 reply_id: 29958[/import]

sounds like a challenge!

i’ve not done it yet, but i’ll see if i can port that code over.

j [import]uid: 6645 topic_id: 3556 reply_id: 29969[/import]

haha,i’m not challenge u, but i really need some help…
to make an object rotate based on our touch…and then with certain speed of our touch force, i wanna make that object spinning based on our touch force, then slowly stopping…:slight_smile: [import]uid: 33180 topic_id: 3556 reply_id: 29970[/import]

Great jmp909, thanks for sharing! [import]uid: 10426 topic_id: 3556 reply_id: 29987[/import]