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 Discussion / Moving and then pasting sprite with a single mouseclick.

Author
Message
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 14th Jul 2008 02:17
I want to move a sprite around in a drawing program I am working on. I want to left click the mouse button hold it down and position the sprite just where I want it and then release the button and the sprite paste at the mousex() and mousey() coords.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 14th Jul 2008 07:41
What you will need to know:

mouseclick()
mousex()
mousey()
sprite

Here is the application:


It is pretty simple. Basically, the sprite is always placed at the coordinates x and y. When you click the mouse, the x and y variables are set to the mouse's x and y positions.

Hope this helps

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 14th Jul 2008 15:28 Edited at: 14th Jul 2008 22:41
I understand what you explained. Here is a short version of the code I am working on. I don't want the sprite to be pasted until I release the left mouse button. The code works, but the speed of the computer is to quick to recognize that that mouseclick()=0. Any ideas on how to do something like this, yet make it work better?

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 14th Jul 2008 18:18
Ok, two things:

1.) See the "Code" button on the top right corner of the box you type your messages in? The one above the smilies? PLEASE HIGHLIGHT YOUR CODE AND PUSH THAT BUTTON SO YOUR CODE IS IN A BOX CALLED "CODE SNIPPIT" !!!

It is much easier to view:



2.)

When typing code, you have this button named "Tab". It should be the fourth from the bottom left upwards. Push that when ever you start an if command, or a loop:



try this:



When you click on the sprite, the sprite will be dragged at the position you clicked on, not at the top left corner. When you let go of the mouse, the sprite will be positioned where you let go.

TheComet

Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 14th Jul 2008 23:31
TheComet thank you for your suggestion. I tried it but did not get the results I am looking for. I reworked the program a little and came up with the following code. There has to be a better way than using the "for next" statement. I'm hoping someone will see the program and know what I am trying to do.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jul 2008 01:35
So you want to drag the sprite around and drop it at new location? Or do you want to use the sprite like a rubber stamp? You click it, get an image of the sprite, then paste that image at a new location? Or do you want a trail of wherever the sprite has been so it's like a paint brush?

From what you said in the top post, It seems you want to drag and drop. But your last coding example implies wanting to use it like a brush. The mouse release problem can be handled using a while loop - checking while mouseclick() <> 0. Anyway, here's the drag and drop method:



Enjoy your day.
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 15th Jul 2008 04:31
Thank you Latch. I am using DBC and DBC had a syntax error with line 42:"if bang > 0". Was your code for DBPro? I have code for draging and leaving a trail. I want to drag a sprite or sprites, without a trail, and place them at a precise location. I would like to do this all with one click and release of the mouse button.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jul 2008 08:41 Edited at: 15th Jul 2008 08:44
That's odd. It's DBC code. Runs fine. I even copied the version I posted here back to my editor and ran it and it worked - both 1.13 and 1.20 .

Not sure what the problem is. I noticed in your code you had a line that looks like:


This returns an error because the rem doesn't have a : in front of it to separate it as it's own line. By any change did the rem in line 43 of my code get moved up at the end of line 42 when you copied it? The only other thing I can think of is the error is in line 41 and for some reason your DBC doesn't like sprite collision()
What version of DBC do you have? You may try changing line 41 to
.

Anyway, the example places ten sprites randomly on the screen. If you click on one, it centers itself on the mouse and you can place it anywhere on the screen. When you let go of the left click, the sprite detaches from the mouse and stays where ever you put it.

It uses a single dot as a sprite that is always positioned at the mouse. Using this dot sprite, you can check for sprite collision with any sprite the mouse is over. When you click the button and there is collision, you enter a WHILE ENDWHILE loop that updates the sprite position with the mouse position until mouseclick()=0. That's basically how it works.

So it essence, you drag the sprite around until you release the mouse button.

Enjoy your day.
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 15th Jul 2008 15:26 Edited at: 15th Jul 2008 23:37
I copied your first code a second time and tried it. It worked this time. For some reason the first time I copied it line 42 read as follows: "if bang <> 0" instead of "if bang > 0". It worked just fine the second time. Yeah! Just what I was looking for. Thank you so very much. I need to study up on sprite collision detection. I do not know much about that yet. Thanks again Latch. Your help has put a big grin on my face.
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 16th Jul 2008 00:39
Latch, I've been looking over your code. I have two questions.

1. When you call the function "drag_sprite(2)", what does the "(2)" represent?

2. In the function itself does the variable "detector" have a value of 2 because of the function call in the "do/loop" section? And if so could you have easily done without a variable at all?
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Jul 2008 21:08
the '2' represents the sprite number you want to drag. This sprite number is referenced by the variable 'detector' in the drag_sprite function.

Don't know if this will help you or not, but this is an inventory system I made awhile back for other folks. However, it is DBP code but you might dig through it and find something useful.





Irojo
17
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 17th Jul 2008 22:09
@Brazos

Even though it's for 3D, TDK's tutorial might help you. Just remember, it's sprite collision, not object collision:


http://forum.thegamecreators.com/?m=forum_view&t=96045&b=10

I urge you to watch the film "Who killed the electric car". Support electric cars! Did you know their used to be more electric cars then gassoline cars?
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 17th Jul 2008 23:07
@Brazos

Quote: "1. When you call the function "drag_sprite(2)", what does the "(2)" represent?

2. In the function itself does the variable "detector" have a value of 2 because of the function call in the "do/loop" section? And if so could you have easily done without a variable at all? "


For number 1, I think Phaelax meant this is the sprite that collides with any other sprite you want to drag. The 2 is for Sprite 2 which I set up as the dot sprite that always is positioned at the mouse. When the mouse is moved, any other sprite that this dot comes into contact with is potentially one of the sprites that will be dragged. So, the "2" represents sprite 2 - the collision test sprite.

For question number 2, yes. "2" is passed to the variable detector in the do loop.
Quote: "And if so could you have easily done without a variable at all?"

I could have if I wanted it hard coded (always to be number 2 as the detector sprite). I left it as a variable so the function could be flexible.

For example: let's say you were making a puzzle program and you reserved sprites 1 through 20 as puzzle pieces you would move around on the screen. If I hard coded the function to always use 2, then one of your puzzle pieces would be moving around with the mouse all the time and be used to detect collision with the other pieces; unless you assigned that single puzzle piece some other number or moved the range of puzzle pieces. I wanted the flexibility to change which sprite I use to test collision.

So you would create a dot sprite that you would use instead that isn't one of your reserved puzzle piece sprites. You might assign sprite 1000 as the detector if you want. By leaving the variable in the function, you can pass any sprite number you want to the function without having to rewrite the function. Of course, you would have to create the sprite before using the function!

Just a note about functions. What's great about functions is the ability to pass variables from the the main program to the function. This allows you to loop the function and change the input every iteration without having to change the function. For example: if I wanted the sum of 2 numbers and the 2 numbers could change at any given time, I might write a function that looks like:

This could be in a loop and every time a or b changes, the result changes. If I hard coded the function to be

then every time I call the function it returns 3.

Often, I create a function with the idea that it is going to be used as a library. A library is a set of functions that is saved as a .dba file. Any program can load in and use the library by calling the #INCLUDE command. This is helpful if you use the same functions over and over. If a function in a library is hard coded (it doesn't use any variables to change the values it performs calculations on), then you either have to use that value in the function or you have to open up the library and change the value in the function each time you want it to be different. Maybe you knew all this, but I just wanted to explain why I use detector as an input parameter in drag_sprite(detector) instead of just making it 2.


Also check out Phaelax's code. It's got some good stuff in there. And of course, check out TDK's tutorials. There's one about menu creation and I believe it goes through sprite selection.

Enjoy your day.
Brazos
22
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 27th Jul 2008 01:11
everyone has been a big help to me. Thanks very much.

Login to post a reply

Server time is: 2025-06-07 06:55:59
Your offset time is: 2025-06-07 06:55:59