Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Unable to drag objects from a Image sheet
Started by kwesijc Dec 13 2017 10:06 AM

4 replies to this topic
touch event drag objects event listener

Best Answer roaminggamer , 13 December 2017 - 02:23 PM

** UPDDATED **

 

Do this:

local showFruits = display.newImageRect( fruitSheet, s[1], 80, 80)
showFruits.x = 50
showFruits.y = 25

function showFruits:touch(event)
   if event.phase == "began" then
      self.markX = self.x
      self.markY = self.y
   elseif event.phase == "moved" then
      local x = (event.x - event.xStart) + self.markX
      local y = (event.y - event.yStart) + self.markY
      self.x, self.y = x, y
   end
end

showFruits:addEventListener("touch")


Get rid of your other listener code.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

kwesijc

[GLOBAL: userInfoPane.html]
kwesijc
  • Observer

  • 11 posts
  • Corona SDK

I am a newbie to Corona SDK. I am trying to drag the fruits on the screen. I am having a "stack traceback" error code on the function "dragFruits..." line. I copy this same block of code on one of the Corona youtube videos. 

 

However, I am stuck and do not know exactly on what to do.

 

Any help will be appreciated. Thanks.

 

local fruitShuffle = require("TableShuffle")
local fruits = require("fruits")
local fruitSheet = graphics.newImageSheet( "fruits.png", fruits:getSheet() )

t = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}
shuffleTable = shuffle(t)


s = {t[1],t[2],t[3],t[4],t[5],t[6]}
shuffleTable = shuffle(s)


function dragFruits:touch(event)


if event.phase == "began" then
self.markX = self.x
self.markY = self.y


elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y
end


return true

end

showFruitsSeqData = { 
{name="S1",sheet=fruitSheet,frames={s[1]} },
{name="S2",sheet=fruitSheet,frames={s[2]} },
{name="S3",sheet=fruitSheet,frames={s[3]} },
{name="S4",sheet=fruitSheet,frames={s[4]} },
{name="S5",sheet=fruitSheet,frames={s[5]} },
{name="S6",sheet=fruitSheet,frames={s[6]} }
}


showFruits = display.newImageRect( fruitSheet, s[1], 80, 80)
showFruits.x = 50
showFruits.y = 25
-- showFruits:addEventListener( "touch", dragFruits )

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,605 posts
  • Corona SDK

  Best Answer

** UPDDATED **

 

Do this:

local showFruits = display.newImageRect( fruitSheet, s[1], 80, 80)
showFruits.x = 50
showFruits.y = 25

function showFruits:touch(event)
   if event.phase == "began" then
      self.markX = self.x
      self.markY = self.y
   elseif event.phase == "moved" then
      local x = (event.x - event.xStart) + self.markX
      local y = (event.y - event.yStart) + self.markY
      self.x, self.y = x, y
   end
end

showFruits:addEventListener("touch")


Get rid of your other listener code.


Edited by roaminggamer, 13 December 2017 - 02:24 PM.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,605 posts
  • Corona SDK

If you read this while I was updating, check my changes above.



[TOPIC: post.html]
#4

kwesijc

[GLOBAL: userInfoPane.html]
kwesijc
  • Observer

  • 11 posts
  • Corona SDK

@roaminggamer Thank you so much. It worked! I have a question. Is the order in which I placed the function matter?


[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,605 posts
  • Corona SDK

You mean in your original example?

 

No, the problem wasn't order but the way you were doing it.




[topic_controls]
[/topic_controls]