I can’t believe i managed to do this so quickly, but in just three hours i managed to rewrite all my code into a single script that gets the input and moves both characters and deals with collisions only using triggers!!
I even had to start tabbing my code at the end like a real pro.
i’ve never done any coding before (except with blocks in stencyl) and I’m really proud of what I’ve achieved with Lua in just 4 days (started on sunday).
here’s my masterpiece:
function init(self)
-- world stuff
global_speed = 0.5 -- LOWER NUMBER IS ACTUALLY FASTER HERE
global_tilesize = 200
--What keys are being pressed
global_up=0
global_down=0
global_left=0
global_right=0
--Are they moving
moving=false
-- get input focus
msg.post(".", "acquire_input_focus")
end
--This code recieves the message that an animation has finished
local function done(self, url, property)
moving=false
end
--THIS CODE TAKES THE INPUT
function on_input(self, action_id, action)
if action_id == hash("up") and action.pressed then
global_up =1
print("input recieved")
elseif action_id == hash("up") and action.released then
global_up =0
end
if action_id == hash("down") and action.pressed then
global_down =1
elseif action_id == hash("down") and action.released then
global_down =0
end
if action_id == hash("left") and action.pressed then
global_left =1
elseif action_id == hash("left") and action.released then
global_left =0
end
if action_id == hash("right") and action.pressed then
global_right =1
elseif action_id == hash("right") and action.released then
global_right =0
end
if action_id == hash("enter") and action.pressed then
end
end
function update(self, dt)
apos = go.get_position("alex")
ipos = go.get_position("isabel")
inputstring = (global_up..global_down..global_left..global_right)
if ipos.x%global_tilesize + ipos.y%global_tilesize==0 then
ialigned=true
end
if apos.x%global_tilesize + apos.y%global_tilesize==0 then
aaligned=true
end
--THIS CODE CREATES A STRING BASED ON THE INPUT AND TURNS IT INTO ONE "direction" VARIABLE (U,D,L,R or 0)
--IT ALSO DOESN'T ALLOW YOU TO CHANGE DIRECTION OR STOP UNLESS ALIGNED TO THE GRID
if inputstring == "1000" then
direction = "u"
auptarget= apos.y + global_tilesize
iuptarget= ipos.y + global_tilesize
elseif inputstring == "0100" then
direction = "d"
adowntarget= apos.y - global_tilesize
idowntarget= ipos.y - global_tilesize
elseif inputstring == "0010" then
direction = "l"
alefttarget= apos.x - global_tilesize
ilefttarget= ipos.x - global_tilesize
elseif inputstring == "0001" then
direction = "r"
arighttarget= apos.x + global_tilesize
irighttarget= ipos.x + global_tilesize
elseif inputstring == "0000" then
direction = "0"
end
print("megatsunami")
print(moving)
print(ialigned)
print(aaligned)
print(direction)
--THIS CODE TURNS THE "direction" variable into movement (WITH ANIMATION IN UP SECTION)
if not moving and ialigned and aaligned then
if direction == "u" then
if not global_atouchingdown then
go.animate("alex", "position.y", go.PLAYBACK_ONCE_FORWARD, auptarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("alex moving up")
end
if not global_itouchingdown then
go.animate("isabel", "position.y", go.PLAYBACK_ONCE_FORWARD, iuptarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("isabel moving up")
end
end
if direction == "d" then
if not global_atouchingup then
go.animate("alex", "position.y", go.PLAYBACK_ONCE_FORWARD, adowntarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("alex moving down")
end
if not global_itouchingup then
go.animate("isabel", "position.y", go.PLAYBACK_ONCE_FORWARD, idowntarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("isabel moving down")
end
end
if direction == "l" then
if not global_atouchingright then
go.animate("alex", "position.x", go.PLAYBACK_ONCE_FORWARD, alefttarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("alex moving left")
end
if not global_itouchingright then
go.animate("isabel", "position.x", go.PLAYBACK_ONCE_FORWARD, ilefttarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("isabel moving left")
end
end
if direction == "r" then
if not global_atouchingleft then
go.animate("alex", "position.x", go.PLAYBACK_ONCE_FORWARD, arighttarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("alex moving right")
end
if not global_itouchingleft then
go.animate("isabel", "position.x", go.PLAYBACK_ONCE_FORWARD, irighttarget, go.EASING_LINEAR, global_speed, 0, done)
moving=true
print("isabel moving right")
end
end
end
end --ends updates self