What is wrong with my drag program?
Author |
Message |
Bo0sT
![](http://compsci.ca/v3/uploads/user_avatars/86196945245bc23817a7f1.jpg)
|
Posted: Sat Jan 27, 2007 11:08 pm Post subject: What is wrong with my drag program? |
|
|
I want to make games with turing but first I want to know how to do things like make dragable objects, iv made a sad attempt and I am hoping someone out there can help me out
code: | setscreen ("graphics:200;200,position:200;200,nobuttonbar,title:test,offscreenonly")
var x, y, b : int
var a, e : int := 10
var c, d : int := 0
loop
drawbox (a, e, c, d, 7)
View.Update
Mouse.Where (x, y, b)
if x > -1 and x < 11 and y > -1 and y < 11 and b = 1 then
a := x
e := y
c := x + 10
d := y + 10
cls
else
end if
end loop |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Bored
![](http://compsci.ca/v3/uploads/user_avatars/7678827054c0296ac1832b.gif)
|
Posted: Sat Jan 27, 2007 11:28 pm Post subject: Re: What is wrong with my drag program? |
|
|
Well your program just simply moves the object to where you click if you click within a certain range (i.e. (-1 - 11, -1 - 11)). What you need to do is have the program first figure out if you are clicking on the box. If so use a boolean value in order to say so. For example:
Turing: | if clicking on box then
drag := true
end if |
Which can then be shortened to
Turing: | drag := clicking on box |
Clicking on box being you compassisons (i.e. and's, or's, ='s, etc.)
Now once clicking on the box you should have it move to wherever the mouse is untill you release the mouse button. That would look something like this.
Turing: | if drag and b >= o then
%Move box to where mouse is
else
drag := false
end if |
The second part simply resets drag to false when the button is released. I'll leave the rest of this up to you to figure out. |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Sun Jan 28, 2007 12:41 pm Post subject: Re: What is wrong with my drag program? |
|
|
You are using constants to determin if the mouse is within the box. Use variables instead, since the box moves it's coordinates won't stay the same; they varry. Use names x1,y1,x2,y2 NOT a,e,c,d. Replace if x > -1 and x < 11 and y > -1 and y < 11 and b = 1 with variables. So it should look like this: Turing: | setscreen ("graphics:200;200,position:200;200,nobuttonbar,title:test,offscreenonly")
var x, y, b : int
var x1, y1, x2, y2 : int := 0
x2 := x1 + 10
y2 := y1 + 10
loop
drawbox (x1, y1, x2, y2, 7)
View.Update
Mouse.Where (x, y, b )
if x > x1 and x < x2 and y > y1 and y < y2 then
if b = 1 then
x1 := x - 5
y1 := y - 5
x2 := x1 + 10
y2 := y1 + 10
cls
end if
end if
end loop |
Now if you run this program and move the mouse quickly, the box wil not move. We can fix this by doing what bored did. Use booleans to determin if you still have the box. Your final code should look like this: Turing: | setscreen ("graphics:200;200,position:200;200,nobuttonbar,title:test,offscreenonly")
var x, y, b : int
var x1, y1, x2, y2 : int := 0
x2 := x1 + 10
y2 := y1 + 10
var buttonPressed : boolean := false
loop
drawbox (x1, y1, x2, y2, 7)
View.Update
Mouse.Where (x, y, b )
if x > x1 and x < x2 and y > y1 and y < y2 then
if b = 1 then
buttonPressed := true
else
buttonPressed := false
end if
end if
if buttonPressed then
x1 := x - 5
y1 := y - 5
x2 := x1 + 10
y2 := y1 + 10
cls
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
|
|