Advertisement

click and drag sprite

Started by January 11, 2025 12:23 AM
18 comments, last by NubDevice 9 hours, 4 minutes ago

here is an image of my problem

As expected.
I assume your program flow is currently like this:

main ()
{
	//...
	DrawBackgroundMap();

	while (quit == false) 
	{
		//...
		DrawSprite();
	}
}

So the background is only drawn once, then the sprite is drawn every frame.

You need to change it like this:

 main ()
{
	//...	

	while (quit == false) 
	{
		//...
		DrawBackgroundMap();
		DrawSprite();
	}
}

So both background and sprite is drawn in desired order each frame.

You see that's actually simple to fix.
If you have the background drawing in a function. Otherwise you may need to move a lot of code and some fixes, i guess.

That's one reason why putting stuff into functions is good and should become a habit.

Advertisement

wow that works, thanks, amazing what a little tweak does.

now all I have to do is move the sprites and clamp them to hex grid by the movement points they have to use

I have one little problem with my code when I move the mouse not over the sprite the sprite still moves, I want the sprite to move only when the mouse is over the sprite, I probably need a little tweak.

pbivens67 said:
I probably need a little tweak.

You need a method to come up with little programs to solve given problems.

For example, ask yourself: ‘If i were the computer, what would i do? Which steps, in what order?’
Then you think of a plan. Some conclusions, decisions and actions. In order. Express it in natural language:

'If the mouse cursor is over the sprite, then set the sprite to follow the cursor.
Otherwise do nothing to the sprite.'

Usually the plan is simple enough we keep it just in our mind before we code it, but you can also write it down to paper. Make notes, or draw control flow diagrams. Something that helps to imagine how the plan is processed in ordered steps.
After that you translate the plan to code.

If the plan is right and the code has no bugs, it will work.
But then it's not some magic ‘little tweak’ to you. Becasue you understand how it works, so there is no magic.

Usually and initially, we have some generic perception about the problem, and some vague idea on how to solve it. For example:

pbivens67 said:
I want the sprite to move only when the mouse is over the sprite

If we take this as the problem definition, it turns out ill posed.
Because this implies: If the sprite moves only if the mouse is over it,
the sprite will stick with the cursor as soon as there is overlap, forever, and there is no more way to detach the sprite from the cursor.

That's not what you want, i'm sure. So your understanding of the problem is too vague, and you need to go into details. You need to think about it, coming up with plans, thinking about what could go wrong and how to handle it, etc.

So let's try if you can improve your question. Think about what you really want, and ask again if needed.

You know, asking the right question mostly reveals the path to the right answer with it, but asking a bad question (related, but not defining the problem) mostly means no solution.

You can analyze how drag and drop on the Windows Desktop works for example. Which user actions happen along the process of dragging an icon to another place. That's the kind of thing you probably want.

Advertisement

pbivens67 said:

wow that works, thanks, amazing what a little tweak does.

Maybe that's not the proper reaction. It seems you got a quick fix from somebody, it suddenly worked, and then you're happy and move on.

Bad mistake, if so. Very bad, bad mistake.

You should not leave the place with happiness, but with enlightenment. Happiness dissipates with time, but wisdom stays.

So what you should do is: In such Eureka moment of ‘it works!’, you must put in the effort to fully understand why and how it works.
So in the future, when variations of the same problem come up again and again, you will be able to solve them quickly using the thing you have just learned.

cool I will study my code

Listen to Joe.
This is the exact same problem as with the slider during the blackjack romp.
Only difference is two free range dimensions instead of one clamped linear dimension.
Cool project btw…It has more expressive potential.

Dev careful. Pixel on board.

Advertisement