Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / How to Move a Sprite with Mouse Clicks

Author
Message
JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 6th Jul 2011 03:25
I am having trouble doing the following:


1. Left click on the sprite.
2. Recognize the sprite has been selected and make the sprite move with the cursor.
3. Left click again to place the sprite to the cursor location, but snap to grid. So, even if they click in the upper right corner of a square, it will place into the square centered in the middle.

I have been working on this process for about 2 weeks in DarkBASIC Professional, but noticed that DarkGDK uses C++ (a language I have used a little before) and I decided to start my project over in DarkGDK, so I have no code to show for. Everything has failed so far. I would greatly appreciate if anyone could help.


Thanks,

Justin

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 6th Jul 2011 15:23
I have two approaches you can use: dbSpriteCollision(sp1,sp2) to test if the mouse sprite is colliding with the sprite you want to move. Or loop through the sprites and test the x,y value against the sprites (this is more useful because you don't have to have an itteration of each sprite, you can just dbPasteSprite(..) where you want it, but you have to keep track of each of them yourself). I use this method in my GUI because games that use sprites tend to need a lot of them.
No matter which method you use to get the collision, just take the difference between the location of the sprite(UL) and the mouse (when you clicked) and apply that to the sprite's x,y until you click again.
Once you click, test the "snaped" position. I'm not sure what you want to do this. Are you simply using a standard grid, or are you using a spot (i.e. like a peg in a hole that can be anywhere on the screen).

The fastest code is the code never written.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Jul 2011 21:24
I suppose you are making a tile-based map/level editor? Here is a simple example program for you, I think it does what you described. Make a "trial.jpg" image or substitute your own sprite name into the program and you can run it.



This program uses a simple coordinate check to see if the mouse is within the sprite. Note that this won't work if there are several sprites on top of each other, because then you need to deal somehow with sprites covering each other, to avoid picking up the lower one. Also, this is for one sprite only, of course you will need to adapt it to your own environment, but since there can only be one moving sprite at a time, it shouldn't be too difficult to adapt. You just need to keep track of which sprite is moving at the moment (e.g. store its ID number instead of the boolean flag that I use in this program).

Login to post a reply

Server time is: 2024-11-19 09:22:28
Your offset time is: 2024-11-19 09:22:28