Hello, I’m trying to create a “Time bar” for a game, I found some example code to shrink a rectangle. If possible, I would like to keep the effect that the image also moves to the left, which makes it look animated.
My problem however, is that when I press the add time button the entire thing jumps to the right, I have a general idea of what is wrong but not how to fix it. I reckon that every time the button is pressed, it adds extra width to the original size. Not sure why it moves the rectangle to the right (There might be some conflicts with the increased width and performWithDelay).
Here’s the code (Cheers for paul07’s code!):
[lua]local timeBar = display.newImageRect( “images/bar.png”, 260, 40 )
timeBar:setReferencePoint(display.TopLeftReferencePoint)
timeBar.x = _W*0.2
timeBar.y = _H*0.3
function loseTime(event)
addTimeBtn = display.newImageRect(“images/addtime.png”, 50, 31)
addTimeBtn.x = _W*0.9
addTimeBtn.y = _H*0.9
addTimeBtn:addEventListener(“tap”, addTime)
timeBar.width = timeBar.width - 2
timeBar:setReferencePoint(display.TopLeftReferencePoint)
timeBar.x = _W*0.2
– timeBar.x = 20
checkTime()
end
gameTimer = timer.performWithDelay(5, loseTime, 0)
function addTime(event)
timeBar.width = timeBar.width + 2
–print(“More Time!”)
end
function checkTime()
if(timeBar.width <= 2) then
textObject = display.newText( “Times Up!”, 50, 50, nil, 24 )
print(“Can’t have enough prints!”)
textObject:setTextColor( 255, 50, 50 )
timer.cancel(gameTimer)
gameTimer = nil
end
end[/lua] [import]uid: 87569 topic_id: 17802 reply_id: 317802[/import]
