How to I spawn a sprite at mouseX, tilemapY? (placing a turret on the ground for platformer game)

0 favourites
  • 5 posts
From the Asset Store
25 high quality isometric ground blocks + sprite sheet
  • Hi guys

    I'm pretty new to Construct, (well, on and off, mostly off, but getting into Construct again recently) and just trying to get my head around a simple thing. I have looked for tutorials that tackle this, but so far have not found any solutions to my problem. It may something so trivial that no one had taken the time to explain, but I can't seem to make any progress on this.

    I have a placeholder at mouse.x, mouse.y. I want to click somewhere above the ground tilemap (game is platformer, side view) and I want to create another sprite where the mouse is, but on the ground, and destroy the placeholder. In other words, X to be still at mouse, but Y to be set wherever the ground tilemap is under the mouse cursor (hope this makes sense)

    Obviously x = Mouse.X, Y = Tilemap.Y has not produced the result I wanted, I have tried other things like Tilemap.X + (Mouse.X), (Tilemap.Y) + (Mouse.Y)

    Another version is to have "invisible" sprites spread out along the ground and then just spawn the turret at their coordinates, but this isn't something that I really want to do.

    Hope you guys understand what I am trying to achieve.

  • So you have one tilemap covering the whole level, and you need to find the first non-empty tile directly below the mouse cursor?

    You can do this in a loop, something like this:

    set cellX to Tilemap.PositionToTileX(Mouse.x)
    set cellY to Tilemap.PositionToTileY(Mouse.y)
    For "n" from cellY to cellY+100
     Tilemap compare tile at (cellX, loopindex) >=0
    	Create turret at Tilemap.TileToPositionX(cellX), Tilemap.TileToPositionY(loopindex) 
    	Stop loop
    
  • This is exactly what I needed! Thanks very much, I really appreciate it!

    I also want to make sure I understand this correctly:

    1. on click, variable cellX gets the value of Tilemap.PositionToTileX(Mouse.x) and celly respectively.

    so Tilemap.PositionToTileX(Mouse.X) = the coordinate of X of tile which is on the same X line as the mouse?

    2. I don't really understand the loop from cellY to celly +100 what it actually does (but indeed works!). can you please explain this part for me a bit, as well.

    I believe I understand the rest.

    Thank you very much again!

  • 1. CellX and CellY are tilemap coordinates (or indices) of the clicked tile under the mouse cursor.

    2. Then you scan 100 tiles down starting from CellY. You can adjust this number depending on your tilemap size. If a non-empty tile is found, create turret there and exit the loop.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks again :)

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