0 Favourites

How do I generate text on a sprite?

  • Hey guys, i'm new to Construct 2 so any help would be appreciated.

    For my game I need to figure out how to generate text on top of my sprites which are essentially blocks. These sprites are generated instances of one original and I have 8, those of which get stored into an array for easier access. As of now, they all generate a random number value when created(1-9 respectively) and I need those numbers to dynamically show up on the blocks upon creation. I've downloaded a behavior plugin called "Text on Sprites", but I'm either not using it right or it's just not working for what I need. Wondering if anybody has some good suggestions as to how I should go about this.

    Thanks in advance!

  • If you store the number in a variable for your sprite then you can do it like this.

    For each Sprite
          System create object Text (Sprite.X, Sprite.Y)
          Set Text.text = Sprite.Number
    [/code:329r6p75]
    
    If you have them in an array you can do it more or less the same way, you just need to get the number from the array instead.
  • Thank you both! I will give it a shot and see what works best!

  • korbaach I appreciate the reply but as I looked over that I would have to restructure my code for it to pertain, thanks a lot though!

    @nimos100 this solution seems to work..although, I have run into a problem where its spawning infinity text, and only on one block. I feel like I've nested the for-each improperly. To help with clarity I will post my current block of code:

    SPAWNX & SPAWNY are global vars ( the initial spot the instances start to spawn)

    System On start of layout
    System For "Y" from 0 - 1
      (sub event)System For "X" from 0 - 3: 
                                                System create object Sprite on layer 0 at ((SPAWNX + (loopindex("X"))*(Sprite.Width + 125)) , (SPAWNY - (loopindex("Y"))*(Sprite.Width + 125)))
                                                Sprite Set value to floor(random(1,10)
                                                Array Insert Sprite.Value at index  0 on X axis
    System For Each Sprite
                                               System Create Object Text on layer 0 at (Sprite(0).X , Sprite(0).Y)  //first location of first instance
                                               Text Set Text to Array.At(0) // first instances storage location
    [/code:xapwssyb]
    
    Sorry for the lengthy reply. Any help is appreciated.
  • You might want to remove the (0) after sprite..

    It really takes away the point of using for each..

  • LittleStain derp...thanks...that actually helped with a bug of the numbers not showing up on each brick. BUT still having the infinite loop issue

  • Thanks everyone. Got it working, appreciate all the links and help!!

  • Are you sure that the For loop "Y" from 0 - 1 is not on the top level so it keeps looping, ain't it suppose to be under the On start of layout? or is it already that in the code?

  • It's fixed haha. There definitely was some different logic my friend and I had to use regarding the looping. For storage,access, random placement reasons we actually split up the looping of the blocks into 4 separate loops and its working fine now. Your code definitely helped us come to a solution though even though I did not use the for each method, simply utilized the text object. TBH, I wasn't aware of the methods it had. You brought that into the light for me!

    Having different issues now though regarding selection/deselection. I've posted to another thread:

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Well tried to give the URL but I guess I don't have that privilege, the topic however is this:

    How do I make a select/deselect operation happen w/ sprites?

  • Assume you are referring to something like selecting units in a RTS game or something?

    Single sprite

    If that's the case the easiest way to do it I think. (Of course depending if you are using multiple selection or just one at the time) But if you just need to select between different sprites, I would use a variable called "Selected_sprite_UID" and here you store the UID of a sprite when you click it for instant.

    And then you always use this variable when you need to work with a sprite so you avoid suddenly picking wrong sprites. Since it can only hold one UID at the time, it will make certain of that.

    For deselecting you can for instant do like this:

    On mouse Right click
    Set Selected_sprite_UID = -1
    [/code:1pfuv83w] 
    Since an object can never have a negative UID that will work fine.
    
    [b]Multiple selection[/b]
    If you are aiming for multiple selection, you can add a Boolean variable to your sprites called "Selected = False"
    
    [code:1pfuv83w]
    On Mouse Left click on Sprite
         Set Sprite.Selected = True
    [/code:1pfuv83w]
    
    Then when you need to do something with them.
    [code:1pfuv83w]
    Sprite.Selected = True
    For each Sprite
          <Do something>
    [/code:1pfuv83w]
    
    Deselecting them:
    [code:1pfuv83w]
    On Mouse right click
    Sprite.Selected = True
         Set Sprite.Selected = False
    [/code:1pfuv83w]
    
    [b]Selection box[/b]
    If you need a Selection box, you can do that with a 9-Patch
    [code:1pfuv83w]
    9-patch overlapping Sprite
         Set Sprite.Selected = True
    [/code:1pfuv83w]
    
    Deselecting is done the same way as with multiple selection.
    
    To make a working selection box there are some tutorials to make it scale with the mouse as you drag in the tutorial section if I remember correct. However its not that difficult to make.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
Sprite Font Generator - v3
368 112,304
RetroDaddy's avatar
RetroDaddy
Unread hot topic
187 45,138
Yann's avatar
Yann
Unread hot topic
0 Favourites
[Plugin] tag text
188 25,443
RBuster's avatar
RBuster