Problem scaling an image (it changes its position)

Hello,

I want to scale an image and then replace it with another in the same place, but when I try it the image moves several pixels from the origin.

This is the code:

local ui = require("ui")  
  
local widthS,heightS=display.contentWidth,display.contentHeight  
local background=display.newRect(0,0,widthS,heightS)  
background:setFillColor(255,255,255)  
local my\_group=display.newGroup()  
local img  
  
local function increase(event)  
 if event.phase == "release" then  
 img:scale(1.5,1.5)  
 end  
end  
  
local function setImage(num) --Here is where all happens  
 local lW=img.parent.width  
 local lH=img.parent.height  
  
 my\_group:remove(img)  
 img=display.newImage("foo\_"..num..".png")  
 my\_group:insert(img)  
 img.x=widthS/2  
 img.y=heightS/2-20  
 img.parent.width=lW  
 img.parent.height=lH  
  
end  
local function pressed1(event)  
 if event.phase=="ended" then  
 setImage(1)  
 end  
end  
local function pressed2(event)  
 if event.phase=="ended" then  
 setImage(2)  
 end  
end  
  
--  
--BUTTONS  
--  
local item1\_button = ui.newButton{  
 default = "item1.png",  
 over = "item1.png",  
 onRelease = pressed1  
}  
local item2\_button = ui.newButton{  
 default = "item2.png",  
 over = "item2.png",  
 onRelease = pressed2  
}  
local increase\_button = ui.newButton{  
 default = "buttonGray.png",  
 over = "buttonGray.png",  
 onEvent = increase,  
 text="Increase!",  
 font=native.systemFont,  
 size=18,  
 emboss = true  
}  
  
item1\_button.x=widthS/2-50  
item1\_button.y=heightS-90  
item2\_button.x=widthS/2+50  
item2\_button.y=heightS-90  
increase\_button.x=widthS/2  
increase\_button.y=heightS-30  
  
img=display.newImage("foo\_1.png")  
my\_group:insert(img)  
img.x=widthS/2  
img.y=heightS/2-20  

What am I doing wrong?
I try several options but i am stuck.
I appreciate any help. [import]uid: 44013 topic_id: 13558 reply_id: 313558[/import]

Nobody can help me?
I think it maybe simple to fix but I am a newbee and I can’t solve it [import]uid: 44013 topic_id: 13558 reply_id: 50177[/import]

I have been also working on this problem again. I put it aside to work on other logic in my app for a month now. But I have returned to this for the past couple days. I think I almost have it figured out.

I create a group scale it and try and move the object. I wanted the object to drag relevent with my touch originaly it would gradually move away. But now I can drag the object and it does stay relevent to my touch but on the second touch the objects position is mutiplied by 2. I am a newb to so maybe someone could chime in on this one and set us strait. Here is my Code

[lua]local localGroup = display.newGroup()
localGroup.xScale = 0.3
localGroup.yScale = 0.3

local circ = display.newCircle(1000, 1000, 100, 100)
localGroup:insert(circ)
local function onTouch( event )

local t = event.target
local phase = event.phase
if “began” == phase then

display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif “moved” == phase then
localX, localY = localGroup:contentToLocal(event.x, event.y)
circ.x = localX
circ.y = localY

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

end
return true
end

localGroup:addEventListener(“touch”,onTouch)

circ:addEventListener(“touch”, onTouch)[/lua] [import]uid: 39088 topic_id: 13558 reply_id: 50196[/import]

Hell ya, :slight_smile:
Ok I just figured it out and updated the code this will allow a object to follow the touch coordinates of a scaled group. I hope this helps. If you or someone reading this knows a better way to do it someone let me know please.

Thanks, to ir8primates for pointing me in the right direction a month ago with contentToLocal.

JM,
Method Mobile Studios [import]uid: 39088 topic_id: 13558 reply_id: 50202[/import]