Discord
Login
Community
DARK THEME

Why won't this work?

Can someone tell me why this won't work?

this is a source file that contains functions which is being called in the main file. it contains a script (which is heavily taken from tutorial) that makes the gun in players position point towards the aim.

its telling me that "gun.turn(aim)" function doesn't exist

initGun = function() gun = object x = player.x y = player.y angle = 0 turn =function(aim) angle=(atan2d(aim.y-y,aim.x-x)) end end

aim = object x = mouse.x y = mouse.y end end

updateGun = function() gun.turn(aim) end

drawGun = function() screen.drawSprite("aim",aim.x,aim.y) screen.setDrawRotation(gun.angle) screen.drawSprite("gun",gun.x,gun.y) screen.setDrawRotation(0) end

Have you called the initGun in the init function? I tried to copy paste your code on my project and it worked. Also be careful because if you set the aim's x and y only in the init the position won't get updated, so you have to change them in the update:

init = function()
  initGun()
end

update = function()
  updateGun()
end

draw = function()
  screen.clear()
  drawGun()
end

// You could also just put these ↓ in another files

initGun = function() 
  gun = object 
    x = player.x 
    y = player.y 
    angle = 0 
    turn = function(aim) angle=(atan2d(aim.y-y,aim.x-x)) end
  end

  aim = object 
    x = mouse.x 
    y = mouse.y 
  end
end

updateGun = function() 
  gun.turn(aim) 
  aim.x = mouse.x  // ADD THIS
  aim.y = mouse.y  // ADD THIS
end

drawGun = function() 
  screen.drawSprite("aim",aim.x,aim.y) 
  screen.setDrawRotation(gun.angle) 
  screen.drawSprite("gun",gun.x,gun.y) 
  screen.setDrawRotation(0) 
end

Thank you! It works now!

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community