How do I make a coin ramdomly appear on BG layout?

  • hi, guys. i am making a simple game where you collect coins. i want a coin to randomly appear within the background layout after its has be collected by the player. player collides with coin, coin get destroyed scoring 1 point for player then immediately 2nd coins appears randomly on map and so on still players dies. thanks

  • You could create another coin instance at location (random(LayoutWidth), random(LayoutHeight)) when the player collides with a coin. Presumely, this should be after when you would do the action destroy coin in the collision event.

    But the random coordinate above will sometimes create the coin at undesirable locations such as at the edge of the layout. This could make the coin appearing only partially on the layout. If you don't like this, you will want to add the size of the coin into account, so:

    Assuming the hotspot of the coin is at the center,

    coinWidth/2 + random(LayoutWidth - coinWidth),

    coinHeight/2 + random(LayoutHeight - coinHeight)

  • thanks for the reply! how exactly do i do that? can you give me a run through cause i am new to this! do i need to create a global variable to do this? so confused. Edit: i figured out how to re spawn the coin after it collides with the player but now the coin sometimes doesn't show on the layout. i think its spawning out side the layout! i tried the code you wrote in your post and it said there in variable named coinwidth and height

  • You are new to this? Have you seen https://www.scirra.com/tutorials/37/beg ... onstruct-2 This is the basic foundation of C2 that you must understand before attempting to make anything else.

    [quote:1nj9zaac]but now the coin sometimes doesn't show on the layout.

    EDIT: just saw your edit. Can you check for the location of the coins? See Debug Preview and check where are the coins. (...or are there no coins?)

  • You are new to this? Have you seen https://www.scirra.com/tutorials/37/beg ... onstruct-2 This is the basic foundation of C2 that you must understand before attempting to make anything else.

    lol i will start straight away

    [quote:3crdxptz]but now the coin sometimes doesn't show on the layout.

    EDIT: just saw your edit. Can you check for the location of the coins? See Debug Preview and check where are the coins. (...or are there no coins?)

    they are spawning out side the layout. i just don't know why!! does the layer matters?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)