Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to move only one object?
Started by Poul Aug 27 2018 03:14 AM

2 replies to this topic
move objects

Best Answer XeduR @Spyric , 27 August 2018 - 03:28 AM

You need to put "return true" at the end of your touch functions, i.e.

 

function base:touch(b)
  if b.phase=="began" then print("Touch base status: true.")
    elseif b.phase=="moved" then base.x=b.x;base.y=b.y;
    elseif b.phase=="ended"  then print("Touch base status: false")
  end
  return true
end

This way the touch won't go through the object and only affects the top most display object.

You probably also want to set focus on the object that you are currently touching. If you don't set focus, then the touch event that is on top will always take priority. You can read about the touch event and how to set focus in the documentation: https://docs.coronalabs.com/api/event/touch/index.html.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Poul

[GLOBAL: userInfoPane.html]
Poul
  • Observer

  • 11 posts
  • Corona SDK

Hello everyone.

I want to move object A and only object A, but when center of this object coincide with center of object B then I can move two obj. and I can't separate objects. All code that I use:

CenterY=display.contentCenterY
CenterX=display.contentCenterX
W=display.contentWidth
H=display.contentHeight

local background=display.newImageRect("Images/background.png",720,1280)
background.x=CenterX
background.y=CenterY

local base=display.newImageRect("Images/2.png",50,50)
base.x=CenterX+100
base.y=CenterY+250

local roof=display.newImageRect("Images/1.png",50,50)
roof.x=CenterX-50
roof.y=CenterY-125

function base:touch(b)
  if b.phase=="began" then print("Touch base status: true.")
    elseif b.phase=="moved" then base.x=b.x;base.y=b.y;
    elseif b.phase=="ended"  then print("Touch base status: false")
  end
end

function roof:touch(r)
    if r.phase=="began" then print("Touch roof status: true.")
      elseif r.phase=="moved"  then roof.x=r.x;roof.y=r.y;
      elseif r.phase=="ended" then print("Touch roof status: false.")
    end
end

  base:addEventListener("touch",move)
  roof:addEventListener("touch",move)


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 299 posts
  • Corona SDK

  Best Answer

You need to put "return true" at the end of your touch functions, i.e.

 

function base:touch(b)
  if b.phase=="began" then print("Touch base status: true.")
    elseif b.phase=="moved" then base.x=b.x;base.y=b.y;
    elseif b.phase=="ended"  then print("Touch base status: false")
  end
  return true
end

This way the touch won't go through the object and only affects the top most display object.

You probably also want to set focus on the object that you are currently touching. If you don't set focus, then the touch event that is on top will always take priority. You can read about the touch event and how to set focus in the documentation: https://docs.coronalabs.com/api/event/touch/index.html.



[TOPIC: post.html]
#3

Poul

[GLOBAL: userInfoPane.html]
Poul
  • Observer

  • 11 posts
  • Corona SDK

Thanks XeduR @Spyric




[topic_controls]
[/topic_controls]