How do I generate text on a sprite?

Get help using Construct 2

Post » Thu May 14, 2015 6:23 pm

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!
B
4
Posts: 16
Reputation: 230

Post » Thu May 14, 2015 7:11 pm

Image
B
65
S
23
G
7
Posts: 1,504
Reputation: 10,894

Post » Thu May 14, 2015 7:12 pm

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

Code: Select all
For each Sprite
      System create object Text (Sprite.X, Sprite.Y)
      Set Text.text = Sprite.Number


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.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Thu May 14, 2015 7:42 pm

Thank you both! I will give it a shot and see what works best!
B
4
Posts: 16
Reputation: 230

Post » Thu May 14, 2015 8:44 pm

@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)

Code: Select all
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


Sorry for the lengthy reply. Any help is appreciated.
B
4
Posts: 16
Reputation: 230

Post » Thu May 14, 2015 9:00 pm

You might want to remove the (0) after sprite..
It really takes away the point of using for each..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Thu May 14, 2015 9:25 pm

@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
B
4
Posts: 16
Reputation: 230

Post » Thu May 14, 2015 10:16 pm

Thanks everyone. Got it working, appreciate all the links and help!!
B
4
Posts: 16
Reputation: 230

Post » Fri May 15, 2015 1:59 am

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?
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,838

Post » Fri May 15, 2015 6:52 am

Message: M0A0THIAS is not yet permitted to post plain text URLS (300 rep required). 1 URLS removed. Why?
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:
B
4
Posts: 16
Reputation: 230

Next

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, David181, dop2000, fens and 13 guests