0 Favourites

How do I Create An Object Outside of the layout randomly?

  • How do I Create An Object Outside of the layout randomly? I tried the following:

    Create spr_enemy at X: random(LayoutWidth) Y: random(LayoutHeight) but this would spawn objects inside the layout Help is greatly appreciated.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Well to spawn on the edge of the layout you can do it with two events:

    system compare choose(0,1) = 0
    --- create object at random(LayoutWidth), choose(0, LayoutHeight)
    else
    ---create object at choose(0, LayoutWidth), random(LayoutHeight)[/code:3rlso663]
    
    You can then modify that to spawn further away from the edge (say 100 pixels) with:
    [code:3rlso663]system compare choose(0,1) = 0
    --- create object at random(-100, LayoutWidth+100), choose(-100, LayoutHeight+100)
    else
    ---create object at choose(-100, LayoutWidth+100), random(-100, LayoutHeight+100)[/code:3rlso663]
    
    Both those however only spawn along the edge of a rectangle.  To spawn in a random area outside the layout you can break up the areas outside into four rectangles in which you can create objects in the same way as inside the layout.  Keep in mind you need to define how big an area to spawn in.
    
    For example the whole area outside the layout up to 100 pixels away:
    [code:3rlso663]
    global number region=0
    every tick
    --- set region to choose(0,1,2,3)
    region = 0
    --- create object at random(-100, LayoutWidth), random(-100, 0)
    region = 1
    ---create object at random(100)+LayoutWidth, random(-100, LayoutHeight)
    region = 2
    --- create object at random(0, LayoutWidth+100), random(100)+ LayoutHeight
    region = 3
    --- create object at random(-100, 0), random(0, LayoutHeight+100)
    [/code:3rlso663]
    
    The formulas should be correct, but I haven't tested it.
    -cheers
  • [quote:3mmbmi49]Create spr_enemy at X: random(LayoutWidth) Y: random(LayoutHeight)

    You kinda answered your own question

    The layout in topleft corner is (0,0) and the bottomright is (LayoutWidth, layoutHeight)

    So every value outside of these are outside the layout.

    (-100, layoutHeight) Would spawn an enemy on a random point on the left side outside the layout.

    (layoutWidth + 100, layoutHeight) Would spawn an enemy on a random point on the Right side outside the layout.

    And the same for top and bottom.

  • Thanks R0J0hound and nimos100. You guys helped solve my problem.

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