I've checked the forum and I couldn't find a solution. I want to place some defined sprites on the screen with random numbers. Is there an easy way to couple a random text to a sprite on the screen. The sprites are 3D block drawings and I like to add text to these blocks. The text is changing from time to time.
if you want to display text, you'll need to use TextField object. Together with TTfont, you can use any font you want, In TextField object, you could also change text dynamically.
Let me say how i will program it if i were you, of course it doesn't mean it is the most optimized and best way:D
-I will create some texts inside some txt file. Every line will be my separate text. -So wherever i want i can read those strings(texts) to some array. -I will give my sprites(assets) following by a an arbitrary number. Like sprite1,sprite2,sprite3. -I will take random number and concatenate it with my sprite asset name like; sprite+random number that i generate. -I will do this method for the array of texts also. -Then i will dynamically add them to stage on the scene init.
Generally I would add the bitmap/sprite/character on the screen and then I just add a textfield on top of it centered on the width and height of the sprite. Like the following. I built a simple function that drops text on any sprite, you could easily use a loop to name them all and add a number etc with it. Here is an example of it working and of course the function.
-- used to add a label to a sprite.function addLabel(parent_sprite, label_text)-- Add a label for debugging.
label = TextField.new(nil, label_text)-- Center the label on parent sprite.
label:setPosition( parent_sprite:getWidth()/2-label:getWidth()/2,
parent_sprite:getHeight()/2+label:getHeight()/2)-- Add label to the parent sprite.
parent_sprite:addChild(label)end-- Create a square shaped sprite.local square_shape = Shape.new()
square_shape:setFillStyle(Shape.NONE)
square_shape:setLineStyle(1, 0x000FF, 2)
square_shape:beginPath()
square_shape:moveTo(0, 0)
square_shape:lineTo(0, 100)
square_shape:lineTo(100, 100)
square_shape:lineTo(100, 0)
square_shape:lineTo(0, 0)
square_shape:endPath()
stage:addChild(square_shape)
addLabel(square_shape, "SQUARE")
Hope it helps
P.S. For a random number you can always use math.random()
Thanks for your great help. The sprites I will put on the screen are rectangles with numbers. These numbers are changing in time and if I touch the rectangle and do a move, both rectangle and number must move as if they are one object. The above sample will do the trick I guess. I will try it out and keep you informed.
Just add event listener on parent_sprite, and move it on the listener function. Than both number and the rectangle will move as you wanted. (In the ex @Teranth shared, label is already child of parent_sprite in your case rectangle. So it is like a holder.)
It works! I am quiet new into Gideros so probably I could write this with less cleaner code. This is what I did with your suggestions: ------------------------------------------------------------------
application:setOrientation(Application.LANDSCAPE_LEFT)local blockSprite = Sprite.new()local font1 = Font.new("arial.txt", "arial.png")-- Used to add a label to a sprite.function addLabel(parent_sprite, label_text)-- Add a label.
label = TextField.new(font1, label_text)-- Center the label on parent sprite.
label:setPosition(parent_sprite:getWidth()/2 - label:getWidth()/2 - 8,
parent_sprite:getHeight()/2 + label:getHeight()/2 + 8)-- Add label to the parent sprite.
parent_sprite:addChild(label)
blockSprite = parent_sprite
end-- The 3D block.local block = Bitmap.new(Texture.new("3D_Block_152x88.png"))
stage:addChild(block)
block:setX(480)
block:setY(320)
addLabel(block, math.random(1000))localfunction onMouseDown(self, event)if self:hitTestPoint(event.x, event.y)then
self.isFocus =true
self.x0 = event.x
self.y0 = event.y
event:stopPropagation()endendlocalfunction onMouseMove(self, event)if self.isFocus thenlocal dx = event.x - self.x0
local dy = event.y - self.y0
self:setX(self:getX() + dx)
self:setY(self:getY() + dy)
self.x0 = event.x
self.y0 = event.y
event:stopPropagation()endendlocalfunction onMouseUp(self, event)if self.isFocus then
self.isFocus =false
event:stopPropagation()endend
blockSprite:addEventListener(Event.MOUSE_DOWN, onMouseDown, blockSprite)
blockSprite:addEventListener(Event.MOUSE_MOVE, onMouseMove, blockSprite)
blockSprite:addEventListener(Event.MOUSE_UP, onMouseUp, blockSprite)
@cherry - so how much are you willing to pay for me to do your homework?
WhiteTree Games - Home, home on the web, where the bits and bytes they do play! #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Comments
In TextField object, you could also change text dynamically.
-I will create some texts inside some txt file. Every line will be my separate text.
-So wherever i want i can read those strings(texts) to some array.
-I will give my sprites(assets) following by a an arbitrary number. Like sprite1,sprite2,sprite3.
-I will take random number and concatenate it with my sprite asset name like;
sprite+random number that i generate.
-I will do this method for the array of texts also.
-Then i will dynamically add them to stage on the scene init.
Hope it is understandable.
Take care
Generally I would add the bitmap/sprite/character on the screen and then I just add a textfield on top of it centered on the width and height of the sprite. Like the following. I built a simple function that drops text on any sprite, you could easily use a loop to name them all and add a number etc with it. Here is an example of it working and of course the function.
P.S. For a random number you can always use math.random()
Thanks for your great help. The sprites I will put on the screen are rectangles with numbers. These numbers are changing in time and if I touch the rectangle and do a move, both rectangle and number must move as if they are one object. The above sample will do the trick I guess. I will try it out and keep you informed.
Best regards,
Marc
Than both number and the rectangle will move as you wanted.
(In the ex @Teranth shared, label is already child of parent_sprite in your case rectangle. So it is like a holder.)
cleaner code. This is what I did with your suggestions:
------------------------------------------------------------------
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill