How do I make draggable windows? I am making a "pong" app with an "X" button, so how do I make it draggable?
Offline
First of all, if you have window contents that are supposed to move, you have to assign each one of them (every sprite) variables for their positions relative to the window. I would use the window top right-hand corner as (0,0). When you modify the window's position - by dragging it - the contents of the window adjust their actual x and y positions based on the position of the top-right corner (which is stored in a pair of variables) and their own relative positions. Here's an example.
when gf clicked // for every window entity forever go to x: ((windowX) + (offsetX)) y: ((windowY) + (offsetY)) end when gf clicked // in main control sprite forever adjust value of windowX and windowY based on mouse input. endInstead of adjusting the values of a window entity's position directly, you modify its offsetX and offsetY values. This corrects its position inside the window.
Last edited by amcerbu (2012-09-04 19:44:49)
Offline
amcerbu wrote:
First of all, if you have window contents that are supposed to move, you have to assign each one of them (every sprite) variables for their positions relative to the window. I would use the window top right-hand corner as (0,0). When you modify the window's position - by dragging it - the contents of the window adjust their actual x and y positions based on the position of the top-right corner (which is stored in a pair of variables) and their own relative positions. Here's an example.
when gf clicked // for every window entity forever go to x: ((windowX) + (offsetX)) y: ((windowY) + (offsetY)) end when gf clicked // in main control sprite forever adjust value of windowX and windowY based on mouse input. endInstead of adjusting the values of a window entity's position directly, you modify its offsetX and offsetY values. This corrects its position inside the window.
What do you mean by "adjust value of blah blah blah"? Can you write out the script?
Offline
This goes in the sprite that is the drag-able part of the window (i.e. the top part, or whatever you think should it should be).
windowX and windowY are the coordinates on the screen of the top left-hand corner of the window (that should be the costume center of the drag-able sprite).
forever if<<<mouse down?> and <(mouseDownBefore) = [false]>> and <touching [mouse pointer v]?>> set [mouseXOffset v] to ((windowX) - (mouse x)) set [mouseYOffset v] to ((windowY) - (mouse y)) set [mouseDownBefore v] to [true] set [dragging? v] to [true] end if<<mouse down?> and <not<touching [mouse pointer v]?>>> set [mouseDownBefore v] to [true] end if<(dragging?) = [true]> set [windowX v] to ((mouseXOffset) + (mouse x)) set [windowY v] to ((mouseYOffset) + (mouse y)) end if<not<mouse down?>> set [mouseDownBefore v] to [false] set [dragging? v] to [false] end end
Last edited by amcerbu (2012-09-04 20:18:35)
Offline
You could just click the "lock" by the sprite (or use the workaround) (see this wiki article
Edit: after, of course, as mentioned above, making all the sprites within the window's position relative to the window.
Last edited by MoreGamesNow (2012-09-04 20:22:57)
Offline
^^ The problem with that is that when using the built-in drag feature in Scratch, positions don't update until after you've released the sprite. So window contents would appear to jump from their original position to the new one. What I posted is basically just a dragging workaround.
Offline
amcerbu wrote:
^^ The problem with that is that when using the built-in drag feature in Scratch, positions don't update until after you've released the sprite. So window contents would appear to jump from their original position to the new one. What I posted is basically just a dragging workaround.
Ahhh, I didn't know that. I usually just use a workaround to give myself greater control.
Offline
amcerbu wrote:
@Firedrake969, is that what you wanted?
Yep.
Last edited by Firedrake969 (2012-09-05 20:07:11)
Offline
That would centre the window on the mouse though.
Offline
Firedrake969 wrote:
amcerbu wrote:
@Firedrake969, is that what you wanted?
Yep.
Just thinking though, I want to be able to have a couple windows open at a time. Is that possible w/ this script?
Offline
The easiest way I could think of would be to create a bunch of lists instead of those variables for window position, etc. Check out this Wikipedia article and I'll help you if you have other questions.
Offline
I used this:
when gf clicked forever set x to ((x pos of window v)+(offsety)) set y to ((y pos of window v)+(offsetx)) endon the red cross and any sprites on the window, and on the window I just made the sprite draggable.
Last edited by northmeister (2012-09-07 17:39:34)
Offline
amcerbu wrote:
A bunch of scratchblocks
Should I set the variables as anything prior to this script?
Offline
bertdog wrote:
you could just do this
when gf clicked if <mouse down> go to [mouse pointer]
This is the most annoying thing I have seen in an OS.
Offline
Put this on the cross sprite.
northmeister wrote:
when gf clicked forever set x to ((x pos of window v)+(offsety)) set y to ((y pos of window v)+(offsetx)) end
And change the window sprite from undraggable to draggable. That works perfectly
Offline
BirdByte wrote:
amcerbu stole the script right outta my noggin. XP
run for the tinfoil hats!!
Offline
@BirdByte: Haha
@northmeister: Yeah, but the window contents won't move in realtime. When you drag a sprite using Scratch's built-in system, it only registers its position change after you've let go of the mouse. Unless the entire window is one sprite (with multiple costumes, for example), it would appear to jump from one place to the next. I assume that Firedrake969 wants to have multiple sprites take part in each window, since he mentioned a pong game earlier.
Last edited by amcerbu (2012-09-15 20:03:21)
Offline
amcerbu wrote:
What do you mean? What's the problem you're having?
I try to drag it and it disappears. What I really want is just being draggable w/o the unlock.
Offline