:scale( x, y ) Acts Weird (Seems To Be Cumulative)?

Hi,

If I do object:scale(5, 5) to a not scaled object

and then later do object:scale(1, 1)

then the object does not return to a not scaled object (original size)?

Seems like the scale is cumulative, how can I change that so object:scale(1, 1) --equals no scale?

Thanks!

JeZxLee

Can you post your code? I suspect there’s something else going on…for instance if you set scale twice in the same frame, you will only see the result of the second setting.

Hi,

Here is my current code:

-- "main.lua" TextScaleDir = 1 local composer = require( "composer" ) composer.gotoScene( "sceneAnimatedTextTest" )

-- "sceneAnimatedTextTest.lua" local composer = require( "composer" ) local scene = composer.newScene() local displayText local displayTextScale local textXscale = 1 -- ------------------------------------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view TextScaleDir = 1 local myRectangle = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) myRectangle.strokeWidth = 5 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 0, 1, 0 ) sceneGroup:insert( myRectangle ) displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", 20 ) displayText:setFillColor( 0, 1, 0 ) sceneGroup:insert( displayText ) displayTextScale = display.newText( "", display.contentCenterX, 50, "Font01.ttf", 30 ) displayTextScale:setFillColor( 0, 0, 0 ) sceneGroup:insert( displayTextScale ) end -- ------------------------------------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then local function onEveryFrame( event ) textXscale = displayText.xScale if TextScaleDir == 1 then if textXscale \< 5 then displayText:scale( 1.01, 1.01 ) else TextScaleDir = 0 -- How to set displayText:scale() to exactly 5 x ? end elseif TextScaleDir == 0 then if textXscale \> 0.01 then displayText:scale( 0.99, 0.99 ) else TextScaleDir = 1 -- How to set displayText:Scale() to exactly 0 x ? end end displayTextScale.text = tostring( textXscale ) end Runtime:addEventListener( "enterFrame", onEveryFrame ) end end -- ------------------------------------------------------------------------------------------------------------- function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- ------------------------------------------------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------------------------------------- return scene

Thanks!

JeZxLee

If you want to come back to a scale 1,1 you should call scale(0.2, 0.2) after scale(5, 5).

You can use object fields xScale and yScale to know the scale of an object. With them you can easily revert to scale 1,1 (or whatever scale you want).

1 Like

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Hi,

Thanks for the reply!

Is there some formula I can use to easily set the scale of and object back to (1, 1) --no scaling/original size?

object:scale( some formula, some other formula)

Thanks!

JeZxLee

You can just do object.xScale = 1.

obj:scale(...) -- is multiplicative

local circ = display.newCircle( 10, 10, 10 ) circ:scale(0.5,0.5) circ:scale(0.5,0.5)

is the same as:

local circ = display.newCircle( 10, 10, 10 ) circ.xScale = 0.25 circ.yScale = 0.25

From Corona documentation

Effectively multiplies the size of a display object by xScale and xScale respectively.

xScale  and xScale are arguments passed to scale function.

So @roaminggamer have right.

Hi,

Got everything working now…

Thanks to all who have helped!

Here is the working code for the demo:

-- "main.lua" TextScale = 1 TextScaleDir = 1 local composer = require( "composer" ) composer.gotoScene( "sceneAnimatedTextTest" )

-- "sceneAnimatedTextTest.lua" local composer = require( "composer" ) local scene = composer.newScene() local displayText local displayTextScale -- ------------------------------------------------------------------------------------------------------------- function round(x, n) n = math.pow(10, n or 0) x = x \* n if x \>= 0 then x = math.floor(x + 0.5) else x = math.ceil(x - 0.5) end return x / n end -- ------------------------------------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view TextScale = 1 TextScaleDir = 1 local myRectangle = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) myRectangle.strokeWidth = 5 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 0, 1, 0 ) sceneGroup:insert( myRectangle ) displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", 20 ) displayText:setFillColor( 0, 1, 0 ) sceneGroup:insert( displayText ) displayTextScale = display.newText( "", display.contentCenterX, 50, "Font01.ttf", 30 ) displayTextScale:setFillColor( 0, 0, 0 ) sceneGroup:insert( displayTextScale ) end -- ------------------------------------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then local function onEveryFrame( event ) if TextScaleDir == 1 then if TextScale \< 5 then TextScale = TextScale + 0.01 TextScale = round( TextScale, 2 ) displayText.xScale = TextScale displayText.yScale = TextScale else TextScaleDir = 0 displayText.xScale = 5; displayText.yScale = 5; end elseif TextScaleDir == 0 then if TextScale \> 0.01 then TextScale = TextScale - 0.01 TextScale = round( TextScale, 2 ) displayText.xScale = TextScale displayText.yScale = TextScale else TextScaleDir = 1 displayText.xScale = 0.01; displayText.yScale = 0.01; end end displayTextScale.text = tostring( TextScale ) end Runtime:addEventListener( "enterFrame", onEveryFrame ) end end -- ------------------------------------------------------------------------------------------------------------- function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- ------------------------------------------------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------------------------------------- return scene

Thanks!

JeZxLee

Can you post your code? I suspect there’s something else going on…for instance if you set scale twice in the same frame, you will only see the result of the second setting.

Hi,

Here is my current code:

-- "main.lua" TextScaleDir = 1 local composer = require( "composer" ) composer.gotoScene( "sceneAnimatedTextTest" )

-- "sceneAnimatedTextTest.lua" local composer = require( "composer" ) local scene = composer.newScene() local displayText local displayTextScale local textXscale = 1 -- ------------------------------------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view TextScaleDir = 1 local myRectangle = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) myRectangle.strokeWidth = 5 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 0, 1, 0 ) sceneGroup:insert( myRectangle ) displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", 20 ) displayText:setFillColor( 0, 1, 0 ) sceneGroup:insert( displayText ) displayTextScale = display.newText( "", display.contentCenterX, 50, "Font01.ttf", 30 ) displayTextScale:setFillColor( 0, 0, 0 ) sceneGroup:insert( displayTextScale ) end -- ------------------------------------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then local function onEveryFrame( event ) textXscale = displayText.xScale if TextScaleDir == 1 then if textXscale \< 5 then displayText:scale( 1.01, 1.01 ) else TextScaleDir = 0 -- How to set displayText:scale() to exactly 5 x ? end elseif TextScaleDir == 0 then if textXscale \> 0.01 then displayText:scale( 0.99, 0.99 ) else TextScaleDir = 1 -- How to set displayText:Scale() to exactly 0 x ? end end displayTextScale.text = tostring( textXscale ) end Runtime:addEventListener( "enterFrame", onEveryFrame ) end end -- ------------------------------------------------------------------------------------------------------------- function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- ------------------------------------------------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------------------------------------- return scene

Thanks!

JeZxLee

If you want to come back to a scale 1,1 you should call scale(0.2, 0.2) after scale(5, 5).

You can use object fields xScale and yScale to know the scale of an object. With them you can easily revert to scale 1,1 (or whatever scale you want).

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.

Ah, I’ve never used :scale, I just set .xScale and .yScale directly.