Passing a sprite (or reference to) to a Function

0 favourites
  • 13 posts
From the Asset Store
Minimal Sprite Font with Stroke for Pixel Art games.
  • 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!

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

  • Cool, that sounds like it may well fit the bill - I will have a look at this plugin! Thanks! <img src="smileys/smiley1.gif" border="0" align="middle" />

  • pass

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

    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

  • 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!

  • 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?


    PS:Sorry for my english but it's not my native language, hope you understand <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Russus

    Just pass the UID as a function parameter:

    <img src="" border="0" />

  • tulamide

    Ok thanks, it works perfectly! <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Thanks Tulamide - this worked for me too!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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!

  • post a capx please

  • Here you are...

    you can find the original code disabled, and the function i am trying to make

    thank you!

  • Hey Hey people sorry for hijacking the post but can someone help me too :/ -

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)