I have a player in the form of a square. He jumps up and down. I want to attach a text to it that will show a score, so that this text moves along with it. How can I do it?
he jumps up and down
he j
I have a player in the form of a square. He jumps up and down. I want to attach a text to it that will show a score, so that this text moves along with it. How can I do it?
he jumps up and down
he j
Use an enterFrame listener.
local bob = display.newRect( 100, 100, 10, 10 ) bob.hp = 100 bob.myLabel = display.newText( "", bob.x, bob.y - 30, native.systemFont, 22 ) function bob.enterFrame( self ) self.myLabel.x = self.x self.myLabel.y = self.y - 30 self.myLabel.text = tostring(self.hp) end Runtime:addEventListner( "enterFrame", bob ) function bob.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end bob:addEventListener( "finalize" )
Or you can create a group and put boby and the text in it.
Like this when you move the group the two object move together
local bobAndScore=display.newGroup() local bob=display.newRect(...) local Score=display.newText(...) bobAndScore:insert(bob) bobAndScore:insert(Score) Score.y=-10 -- like this the score will be display on the player head bobAndScore.x=100 --bob and the score move
Use an enterFrame listener.
local bob = display.newRect( 100, 100, 10, 10 ) bob.hp = 100 bob.myLabel = display.newText( "", bob.x, bob.y - 30, native.systemFont, 22 ) function bob.enterFrame( self ) self.myLabel.x = self.x self.myLabel.y = self.y - 30 self.myLabel.text = tostring(self.hp) end Runtime:addEventListner( "enterFrame", bob ) function bob.finalize( self ) Runtime:removeEventListener( "enterFrame", self ) end bob:addEventListener( "finalize" )
Or you can create a group and put boby and the text in it.
Like this when you move the group the two object move together
local bobAndScore=display.newGroup() local bob=display.newRect(...) local Score=display.newText(...) bobAndScore:insert(bob) bobAndScore:insert(Score) Score.y=-10 -- like this the score will be display on the player head bobAndScore.x=100 --bob and the score move