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.

DarkBASIC Professional Discussion / 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: 4th Jul 2011 19:54
I am having trouble doing the following:

- Recognize that a particular Sprite has been left clicked
- Move that particular Sprite to the location of the cursor when the left mouse button is clicked again


I have been working on this process for about a week and have no code to show for. Everything has failed. I would greatly appreciate if anyone could help.

Thanks,

Justin
Mobiius
Valued Member
22
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 4th Jul 2011 20:29
Have you written any code to start us off?

My signature is NOT a moderator plaything! Stop changing it!
JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 4th Jul 2011 20:43 Edited at: 4th Jul 2011 21:14
Yes, I have a little bit of code. I have been deleting my failed work, so all I have is a sprite loaded for the 8x6 grid and a sprite for a player tile.



Here are the two images I created:



Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 5th Jul 2011 11:25 Edited at: 5th Jul 2011 11:28
It's tricky to advise as I'm not entirely clear on what you're trying to achieve. For example do you want the player sprite to snap to the grid or just to be moved to where the mouse button is clicked? Also do you want the player to jump to the position of the mouse click or to move smoothly there? At what stage are you having problems with this? Loading and manipulating the sprites? Catching the mouse clicks?

Anyhow here are some code snippets that might be worth playing around with to achieve what you want. To catch a mouse click on a sprite (assuming it has not been offset/rotated etc).



To move a sprite to the position of a mouse click.



or to be centred on a mouse click.



Hope this helps. If you have any follows up, please do ask although giving more details of what you want to achieve will help us give useful answers.

JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 5th Jul 2011 16:40
Lucas Tiridath, you just reminded me that I hadn't really explained everything in detail, so here it is.

Yes, I want the player tile to snap to grid, as it is a grid based game. I also want the tile to go straight to the grid, at this point.

Maybe if I can get farther into development and want things to be more graphically pleasing I will mess with smooth movement, or even chess-like movement where it follows the grid as it moves to the location of the second left mouse click.


So here is my entire process of what I am trying to acheieve:

1. Left click on the sprite.
2. Recognize the sprite has been selected, and perhaps load a message to the player telling them they have clicked it.
3. Left click again to place the player tile to the cursor location, but snap to grid. So, even if they click in the upper right corner of a square, it will just place into the square. At this moment, the movement of the sprite will be that it jumps right to the square.


By the way, the player tile sprite is 100x100 pixels and each grid square is 100x100 pixels. That might be important information.
Gencheff
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location: UK by way of USSR
Posted: 5th Jul 2011 22:26 Edited at: 5th Jul 2011 22:27
Didn't quite read everything,however might I suggest something:

- Create a sprite and in your loop , position it where the mouse is.Then either use that sprite as your cursor,or just hide it.
- Check for collision between that sprite and the rest of your sprites using a loop and add a mouseclick()=1 condition (Check your documentation for the commands).
- Assign the clicked sprite's id to a variable and then if the mouse is clicked again,check for collision with the rest of your sprites and position the selected previously sprite to the x and y of the newly selected (grid) sprite.

Bit of pseudo-code:



Hope this helps,and I hope I've understood your question/s

Samotnijat vylk nasred gorata.
-3D,2D Artist,Animator,Web developer and Programmer.
JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 5th Jul 2011 22:33
Now, since I am very new to DarkBASIC code, I cannot look at a source code and automatically figure out what it means.

With that said, just so I don't waste any time, I want to eventually have many different player tile sprites. It will be for multiple people to play. Will this code work for different images or just the one that I load at the start?

Gencheff
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location: UK by way of USSR
Posted: 6th Jul 2011 01:04
This is pseudo-code,meaning it will not really compile,unless you convert it to actual DBPro commands.

Now,I've included a very simple application with a snap-to-grid function.Tell me if this is what you are after.

Samotnijat vylk nasred gorata.
-3D,2D Artist,Animator,Web developer and Programmer.

Attachments

Login to view attachments
JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 6th Jul 2011 01:13
That is basically what I am trying to do. The only thing is that I want to left click again to drop it, not right click. Could you include the source code for me to use or is that for me to figure out?

Gencheff
14
Years of Service
User Offline
Joined: 12th Jun 2010
Location: UK by way of USSR
Posted: 6th Jul 2011 01:31
Sure,I've just modified it to work with left click only.
The code is messy,I'm warning you,and you have to modify it heavily to make it usable.

Samotnijat vylk nasred gorata.
-3D,2D Artist,Animator,Web developer and Programmer.

Attachments

Login to view attachments
JustinPBanko
13
Years of Service
User Offline
Joined: 4th Jul 2011
Location: Pennsylvania, United States
Posted: 6th Jul 2011 01:57
Wow. There is so much code.

I am a complete noob at this stuff, as you can already tell.

I'm wondering...have you ever played a sandbox game? Where there is a randomly generated world, or even pre-made, and you go out and explore to gather materials then make items out of the gathered materials? I am trying to work on a project like that now.

Do you have any idea as to how something like that could be started? Perhaps we can talk via email, so this thread stays relevant to the original topic.

Login to post a reply

Server time is: 2025-05-28 20:52:41
Your offset time is: 2025-05-28 20:52:41