|
Post by Ben on Feb 18, 2017 8:08:38 GMT
Hello,
I'm using a mouse to set the new direction for a sprite, then I'm using new values to move the sprite a certain amount in the new direction. My problem is that the sprite always ends up where the sprite x y position is. Is it my code, or something else?
ResizeSpriteArea(400,500)
TextMode(TEXT_BUFFERED)
BACKGROUNDCOLOR(0,0,0)
'loading generic sprite for testing
dim g= LoadTex("gfx/Ball.png")
dim c1= NewSprite(g)
SprSetSize(8,8)
SprSetPos(200,250)
dim d1=NewSprite(g)
SprSetSize(8,8)
SprSetPos(210,260)
dim mx,my,r as double,sx as double,sy as double,cx,cy,radA as double,dx as double,dy as double
'starting centor location
cx=200
cy=250
r=50 'radius
while true
'get mouse location
mx = Mouse_X() * 400
my = Mouse_Y() * 500
'convert to radian angle
radA = atn2(mx-cx,my-cy)
'convert to cos and sin values
dx=sin(radA)
dy=cos(radA)
'set angle radains * radius
sx=cx+(dx*r)
sy=cy+(dy*r)
if Mouse_Button(MOUSE_LBUTTON) then
'set distance to move center sprite
cx=cx+(dx*r)
cy=cy+(dy*r)
'move center sprite
bindsprite(c1)
sprsetpos(cx,cy)
'go back to direction sprite
bindsprite(d1)
'show new values of center sprite
locate 0,0:print "cx: ";cx;" cy: ";cy
locate 0,1:print "mx: ";mx;" my: ";my
endif
'set new dir sprite location
sprsetpos(sx,sy)
'draw it.
DrawText()
wend
|
|
|
Post by Ben on Feb 18, 2017 8:09:52 GMT
Sorry, I meant to say the the sprite always moves to where the Mouse x y location is. This is not what I want to happen. I don't think it's my code.
|
|
|
Post by Tom on Feb 19, 2017 7:45:54 GMT
Sorry, I meant to say the the sprite always moves to where the Mouse x y location is. This is not what I want to happen. I don't think it's my code. Hi Ben. I think the problem is that Mouse_Button(MOUSE_LBUTTON) will keep returning true for as long as the button is being held down. Whereas you only want to move your center point when the button is first pressed. I've added a couple of variables to detect this case. Basically it tracks whether the mouse button was pressed the last time around the loop. If it wasn't pressed last time but is pressed now, then you have a button click, and can move your point. ResizeSpriteArea(400,500) TextMode(TEXT_BUFFERED) BACKGROUNDCOLOR(0,0,0)
'loading generic sprite for testing dim g= LoadTex("gfx/Ball.png") dim c1= NewSprite(g) SprSetSize(8,8) SprSetPos(200,250)
dim d1=NewSprite(g) SprSetSize(8,8) SprSetPos(210,260)
dim mx,my,r as double,sx as double,sy as double,cx,cy,radA as double,dx as double,dy as double dim buttonDown, buttonWasDown
'starting centor location cx=200 cy=250 r=50 'radius
while true
'get mouse location mx = Mouse_X() * 400 my = Mouse_Y() * 500
'convert to radian angle radA = atn2(mx-cx,my-cy)
'convert to cos and sin values dx=sin(radA) dy=cos(radA)
'set angle radains * radius sx=cx+(dx*r) sy=cy+(dy*r)
buttonDown = Mouse_Button(MOUSE_LBUTTON) if buttonDown and not buttonWasDown then ' New click
'set distance to move center sprite cx=cx+(dx*r) cy=cy+(dy*r)
'move center sprite bindsprite(c1) sprsetpos(cx,cy)
'go back to direction sprite bindsprite(d1)
'show new values of center sprite locate 0,0:print "cx: ";cx;" cy: ";cy locate 0,1:print "mx: ";mx;" my: ";my endif
'set new dir sprite location sprsetpos(sx,sy)
'draw it. DrawText()
buttonWasDown = buttonDown wend
|
|
|
Post by Ben on Feb 20, 2017 1:43:34 GMT
Hi,
Thank you very much for helping me with this fix. I was thinking it was something like what you explained going on, but was not able to figure out. Thanks again.
Ben
|
|