user3215624
user3215624

Reputation: 55

Getting coordinates of an object in corona SDK game

In my corona application, I have a character that can fly during the gameplay. This is accomplished by touching a jump/fly button. I have achieved this as:

local c=false  -- Flag residing fly/jump button state

function up:touch(event)
  if event.phase == "began"
    c=true 
  elseif event.phase =="ended" then 
    c=false
  end
end

function jump(event)
  if c  then
    character:applyForce (0,-300, character.x, character.y)
  end
end

up:addEventListener( "touch", up )
Runtime:addEventListener("enterFrame", jump)

Now I need to disable the button until the character reaches the ground again. For that, I've decided to check the character Y position on runtime, but don't know how to do that. I'm not sure whether this is the correct idea or not. If you have a better option then please suggest me that also.

Problem:

  1. I want to make function that would calculate object.x and object.y coordinates and saving them to x and y variables all the time when app is running.
  2. I'm starter in lua, is it there any way to program in c++ in corona?

Now I want to check disable the jump button once pressed.

Upvotes: 0

Views: 1382

Answers (1)

Fayer
Fayer

Reputation: 69

This does what you are asking

local objX, objY
local obj

--this runs every frame
local function onEnterFrame( event )
   objX = obj.x
   objY = obj.y 
end

Runtime:addEventListener( "enterFrame", onEnterFrame )

And for the C++ part, I don't think you can do that, Corona's scripting is in Lua. read this to learn Lua

Upvotes: 2

Related Questions