Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sprites with random text — Gideros Forum

Sprites with random text

DikkesnoekDikkesnoek Member
edited July 2012 in Game & application design
Hello,

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.

Thanks,

Marc

Dislikes: Yan

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    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.

    Hope it is understandable.

    Take care
  • By the way if your texts are not so much you can already store inside lua not inside the txt file. It depends on your choice.
  • TeranthTeranth Member
    edited July 2012
    You can easily enough add a number to a sprite.

    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()
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • Hello friends,

    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
  • 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.)
  • Thanks. I've just finished some sprites to test with. I will work on it this afternoon.
  • DikkesnoekDikkesnoek Member
    edited July 2012
    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))
     
    local function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
    		self.x0 = event.x
    		self.y0 = event.y
    		event:stopPropagation()
    	end
    end
     
    local function onMouseMove(self, event)
    	if self.isFocus then
    		local 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()
    	end
    end
     
    local function onMouseUp(self, event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    	end
    end
     
    blockSprite:addEventListener(Event.MOUSE_DOWN, onMouseDown, blockSprite)
    blockSprite:addEventListener(Event.MOUSE_MOVE, onMouseMove, blockSprite)
    blockSprite:addEventListener(Event.MOUSE_UP, onMouseUp, blockSprite)
  • guys,,help me...i need codes for random text in gideros.....badly needed for thesis......thanks
  • @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
Sign In or Register to comment.