How do I create a copy of a sprite and set it to be the size of the sprite it copied?

0 favourites
  • 9 posts
From the Asset Store
Awardkit
$3.50 USD
30% off
Create custom awards and achievement or use the many templates and create your own.
  • Hi all,

    I am creating a game wherein pressing "Z" decreases the sprite size by half and then creates a duplicate of itself. Essentially being split in two.

    I have been able to reduce the size of the sprite by half but when I create the new sprite object it is set to the initial size of the sprite, not the updated half sized sprite.

    Is there any way to set the sprite that is being created to the current size of the sprite it is a copy of?

    I am trying to use the "create object" function for this but what would really be more useful is "duplicate object" or something here.

    Anyway any help is greatly appreciated.

    Thanks!

    Tagged:

  • There are several methods, the easiest would probably be using a pair of local variables:

    On Z pressed
    Cursor is over Sprite (or some other condition which picks one sprite)
    
    ...Local variable w
    ...Local variable h
    
    ......Set w to Sprite.width
    ......Set h to Sprite.height
    ......Sprite Set size to (w/2,h/2)
    
    ......Create New Sprite
    ......Sprite Set size to (w/2,h/2)
    
  • dop2000 Thanks so much for the response!

    I've tried this local variable method and it seems like it should work but for some reason it isn't.

    The problem seems to be with "Sprite Set size to (w/2,h/2)". For some reason this just makes the sprite disappear.

    I know that the local variables are being set correctly though because if I do "Sprite Set size to (w-16,h-16)", the Sprite doesn't disappear and reduces from 32px to 16px correctly.

    Unfortunately I need the size to continue to reduce on each key press which your method should achieve.

    Any idea why the sprite might be disappearing?

    I have attached an image as reference.

    Thanks for your help!

  • No, you can't use local variables like that. Local variables can only store their value until the end of the tick (unless you set them as static). That's why your object disappears - by the time you press Z button, both H and W are 0. And when you do "Sprite Set size to (w-16,h-16)", you set width and height to negative -16.

    If you change the variables to global (move outside the group), your code should work.

    Here is what I meant - local variables are used for temporary storage and only inside this event:

  • Thanks again dop2000 you are a saint!

    I'm trying to switch up my config to match what you have but it won't let me create a "set value" sub event below the local variables.

    How did you do that?

  • dop added a Blank sub-event to the keyboard event. Then you can add the actions...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks AllanR

    When I try that Construct can't find the local variables.

    I have attached an image for reference.

    Thanks for your help!

  • Select your keyboard event and press B. This will create a sub-event. The variables and the rest of the code should be in the sub-event.

  • That did it dop2000

    You are the best kind of person!

    Thanks again so much!

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