when trying to scale the object down on both axis the image will look skewed like it will stretch more on 1 axis. I called object.Xscale=-1 to flip it. Although it works it will cause object:Scale(x,y) to have a problem with scaling. I removed the object.xScale and there seems to be no scaling issue with the display object. What can i do to solve this?
Try using xScale and yScale at the same time with the same value.
Never use -1 when scaling.
i used -1 to flip the display object not to scale it , i have tried what u mentioned but it still skews the image
Hi @gordon_95,
Mainly, you should understand is the difference between “.xScale”/".yScale" and “:scale()”. The first two explicitly set the scale along either the X or Y axis. The second (object:scale(x,y)) multiplies the current scale factors based on the current scale.
Consider this example:
[lua]
local star = display.newImage( “star.png” )
star.x = 100
star.y = 200
star.xScale = 0.5
star:scale( 0.5, 1 )
[/lua]
Run that and you’ll notice that the star is initially scaled to X = 50% by usage of “star.xScale = 0.5”. After that, the star is scaled by an additional 50% by calling “star:scale( 0.5, 1 )”. That results in the star actually being 25% of its initial width (0.5*0.5=0.25).
Now consider the same example with one additional line:
[lua]
local star = display.newImage( “star.png” )
star.x = 100
star.y = 200
star.xScale = 0.5
star:scale( 0.5, 1 )
star.xScale = 0.5 – RESET the scale to 0.5
[/lua]
With the final additional line, the scale is reset to 0.5, so the star is once again 50% of its original width. That is what I mean by the “.xScale” property being a “setter” property for the scale, not a “multiplier” function of the scale.
Hope this helps,
Brent
Hello thank you for you help but i am still confused as to how i can apply this to my situation.
currently i am using object.xscale=-1 to flip my image to face the left
as the image is too big i would need to use object:scale(1,1) or whatever x ,y to scale it to my ideal size
so when i apply the object:scale it causes the image to stretch too much on 1 side
as the post above mentioned that i would need to mutiply back the scale how can i apply it to my situation>?
Hi gordon_95.
Please show some of your codes so we can understand what is your problems.
AttrAnimList[ANIM_TYPE.RUNNING] =
{
sheet = graphics.newImageSheet( “images/NPC_running.png”, {width = 410,height = 332,numFrames = 10} ),
sequence =
{
{name=“npc-running”,start= 1,count =10,time = 500,loopCount = 0, loopDirection = “forward”}
},
}
this above script is in another file where all animations are in there and i will call the needed animation in each scene
the script below is where i call the animation to a local variable and set it to play
local AttrAnim = require(“AttrAnimList”)
running=display.newSprite( AttrAnim[ANIM_TYPE.RUNNING].sheet, AttrAnim[ANIM_TYPE.RUNNING].sequence )
running:setSequence( “npc-running” )
running:play( )
From here i would need to scale the animation so i used running:scale(0.3,0.3). And to make it look at other direction i would use the running.xScale=-1 to flip it to face left(originally facing right).
running:scale(0.3,0.3)
if(dpad.value>180)then
running.xScale=-1
end
to flip back to facing right, i would take a value from the dpad
if(dpad.value<180)
then
running.xScale=1
end
this would allow it to flip but once i called the running.xScale it will skew the image. The image will be stretch in the X-axis and there is no possible way to scale the x-axis no matter what value i put in!
and when i remove the flipping the image (running.xScale=-1 or 1) it will be able to scale properly just that it does not flip.
how should i fix this error and is there another way to flip Images?
thank you
Please note to use tag [code] when you post code to forum.
I think you should read Brent’s post carefully, it has everything about your problem.
First, when you use:
running:scale(0.3,0.3)
Your Running Sprite will have xScale = 0.3 and yScale = 0.3 ( when you create two values at 1,1 )
Then you flip it :
running.xScale=-1
So now, it has xScale = -1 and yScale = 0.3 !!! Of course, you will see it stretch in the X-axis. In this situation,
it must have xScale = -0.3 and yScale = 0.3
You can flip object in two ways:
running.xScale = - running.xScale
or
running:scale(-1 , 1 )
Remember that: obj:scale( i , i ) <=> obj.xScale = obj.xScale * i , obj.yScale = obj.yScale * i
Try using xScale and yScale at the same time with the same value.
Never use -1 when scaling.
i used -1 to flip the display object not to scale it , i have tried what u mentioned but it still skews the image
Hi @gordon_95,
Mainly, you should understand is the difference between “.xScale”/".yScale" and “:scale()”. The first two explicitly set the scale along either the X or Y axis. The second (object:scale(x,y)) multiplies the current scale factors based on the current scale.
Consider this example:
[lua]
local star = display.newImage( “star.png” )
star.x = 100
star.y = 200
star.xScale = 0.5
star:scale( 0.5, 1 )
[/lua]
Run that and you’ll notice that the star is initially scaled to X = 50% by usage of “star.xScale = 0.5”. After that, the star is scaled by an additional 50% by calling “star:scale( 0.5, 1 )”. That results in the star actually being 25% of its initial width (0.5*0.5=0.25).
Now consider the same example with one additional line:
[lua]
local star = display.newImage( “star.png” )
star.x = 100
star.y = 200
star.xScale = 0.5
star:scale( 0.5, 1 )
star.xScale = 0.5 – RESET the scale to 0.5
[/lua]
With the final additional line, the scale is reset to 0.5, so the star is once again 50% of its original width. That is what I mean by the “.xScale” property being a “setter” property for the scale, not a “multiplier” function of the scale.
Hope this helps,
Brent
Hello thank you for you help but i am still confused as to how i can apply this to my situation.
currently i am using object.xscale=-1 to flip my image to face the left
as the image is too big i would need to use object:scale(1,1) or whatever x ,y to scale it to my ideal size
so when i apply the object:scale it causes the image to stretch too much on 1 side
as the post above mentioned that i would need to mutiply back the scale how can i apply it to my situation>?
Hi gordon_95.
Please show some of your codes so we can understand what is your problems.
AttrAnimList[ANIM_TYPE.RUNNING] =
{
sheet = graphics.newImageSheet( “images/NPC_running.png”, {width = 410,height = 332,numFrames = 10} ),
sequence =
{
{name=“npc-running”,start= 1,count =10,time = 500,loopCount = 0, loopDirection = “forward”}
},
}
this above script is in another file where all animations are in there and i will call the needed animation in each scene
the script below is where i call the animation to a local variable and set it to play
local AttrAnim = require(“AttrAnimList”)
running=display.newSprite( AttrAnim[ANIM_TYPE.RUNNING].sheet, AttrAnim[ANIM_TYPE.RUNNING].sequence )
running:setSequence( “npc-running” )
running:play( )
From here i would need to scale the animation so i used running:scale(0.3,0.3). And to make it look at other direction i would use the running.xScale=-1 to flip it to face left(originally facing right).
running:scale(0.3,0.3)
if(dpad.value>180)then
running.xScale=-1
end
to flip back to facing right, i would take a value from the dpad
if(dpad.value<180)
then
running.xScale=1
end
this would allow it to flip but once i called the running.xScale it will skew the image. The image will be stretch in the X-axis and there is no possible way to scale the x-axis no matter what value i put in!
and when i remove the flipping the image (running.xScale=-1 or 1) it will be able to scale properly just that it does not flip.
how should i fix this error and is there another way to flip Images?
thank you
Please note to use tag [code] when you post code to forum.
I think you should read Brent’s post carefully, it has everything about your problem.
First, when you use:
running:scale(0.3,0.3)
Your Running Sprite will have xScale = 0.3 and yScale = 0.3 ( when you create two values at 1,1 )
Then you flip it :
running.xScale=-1
So now, it has xScale = -1 and yScale = 0.3 !!! Of course, you will see it stretch in the X-axis. In this situation,
it must have xScale = -0.3 and yScale = 0.3
You can flip object in two ways:
running.xScale = - running.xScale
or
running:scale(-1 , 1 )
Remember that: obj:scale( i , i ) <=> obj.xScale = obj.xScale * i , obj.yScale = obj.yScale * i