Passing a sprite (or reference to) to a Function

Get help using Construct 2

Post » Fri Aug 09, 2013 12:52 pm

Hi all, I'm stuck on a way to get a function to reference a particular sprite which will change each time the function is called.

I have a game which features small mini games for kids. At the end of each game a pop up window appears to say "well done! you made 3 cup cakes!" or whatever the child is doing.

I created a function to create the pop up and reference the appropriate text from a dictionary so that all the mini games can use the same function and just pass parameters for the text string required, and also pass a parameter of the score value. This all works fine but now I'm trying to get clever and show the score number not as text but as a graphic - i.e. a sprite - so it looks big and pretty and has animation.

The problem is how to pass the number sprite to the function so it can create and position it in the pop up window. I have 10 Sprites - the numbers 0 to 9. AFAIK I cannot pass a sprite (or other object) in a function parameter.

I tried grouping the sprites in a family, and giving the family an instance variable which has the number the sprite represents (so the number 9 is set to 9, for example). Then I passed the score value as a numeric parameter and tried a Pick by evaluation, comparing the instance variable to the passed score parameter which should pick the right sprite, but after that I'm a bit stumped how to get that to show and position the sprite. the Create function just creates a random member of the family even after the picking process.

Any other suggestions to pass a sprite to be created/positioned to a function?

Thanks for any advice!
B
3
Posts: 4
Reputation: 208

Post » Fri Aug 09, 2013 2:16 pm

Have you tried using the Sprite Font plugin?
Make your pop up window function spawn a Sprite Font object, and set the text of the Sprite Font instance to the variable that keeps the score.
B
28
S
10
G
10
Posts: 96
Reputation: 6,582

Post » Fri Aug 09, 2013 3:22 pm

Cool, that sounds like it may well fit the bill - I will have a look at this plugin! Thanks!
B
3
Posts: 4
Reputation: 208

Post » Fri Aug 09, 2013 4:33 pm

pass
[QUOTE=EatMoreSpuds] Any other suggestions to pass a sprite to be created/positioned to a function?
[/QUOTE]
didnt fully understood what your issue(s) was, but if I understood correctly:

pass to function MyFunction
parameter 0: sprite.X
parameter 1: sprite.Y

the function:
myFunction: create sprite on: Set Position
x: call 0
y: call 1
B
18
S
5
G
4
Posts: 568
Reputation: 5,079

Post » Tue Aug 20, 2013 10:58 am

Thanks for the reply California - help is much appreciated! I wanted to pass the sprite itself as well as its position, as the function didn't know which specific sprite to create (to keep it generic)

I did find a cool little work around actually, instead of using separate sprites for each number, I put all the different graphics into ONE sprite, as frames of an animation. Now I can create the same sprite for all numbers, and pass the frame number to show as a parameter. Not useful for all cases you want to pass an object to a function but works in this case!

Thanks for the help all!
B
3
Posts: 4
Reputation: 208

Post » Wed Sep 25, 2013 6:09 pm

Hi,

I'm trying to create a function that recieves an Sprite Object as Parameter but I don't know how to do it (if it's possible)

What I want to do is a function that triggers some actions each time a weapon hits an enemy. My game have more than one weapon so I don't want to repeat the 'Hit Enemy' actions for each weapon.

So what i need is a function that recieves an enemy as parameter and then trigger some actions on it.

Can anybody help me?

Thanks!

PS:Sorry for my english but it's not my native language, hope you understand
B
3
Posts: 2
Reputation: 179

Post » Wed Sep 25, 2013 10:32 pm

@Russus
Just pass the UID as a function parameter:

Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Thu Sep 26, 2013 4:10 pm

@Tulamide

Ok thanks, it works perfectly!
B
3
Posts: 2
Reputation: 179

Post » Mon Feb 24, 2014 1:43 am

Thanks Tulamide - this worked for me too!
B
4
Posts: 10
Reputation: 220

Post » Sat Nov 29, 2014 7:39 pm

Hi there, i am making a kind of quiz game based on textboxes, and need to pass the textBox as a parameter to a function in order to assign the same css styles to every textBox, and also add the same functionality to each when the answer is correct. I can repeat the code for every textBox object, but rather using functions. However, i tried the solution explained above and it doesn´t works fine (i created an aux textBox outside of the layout, assigned the instance passed by parameters with UID, and made changes to that aux textBox).
Could you please help me? thank you!
B
9
S
2
Posts: 10
Reputation: 910

Next

Return to How do I....?

Who is online

Users browsing this forum: Google [Bot], jwilkins, maordany, Phacanu and 10 guests