GameSalad

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to move an actor by dragging it

Hi, my game involves you moving an actor to dodge incoming enemy actors by dragging your actor. I assume that its something with touch is pressed (because that's finger on touchscreen and mouse) to move it, but I don't know how. Any help is appreciated. Thanks.

Comments

  • SocksSocks London, UK.Posts: 12,822Member

    When touch is pressed:

    Constrain x position to mouse x
    Constrain y position to mouse y

  • MeepedMeeped Posts: 31Member

    @Socks said:
    When touch is pressed:

    Constrain x position to mouse x
    Constrain y position to mouse y

    Thanks, it works, but there's a small problem. When I press down, the mouse arrow is off the image and it moves the actor, but they're out of sync together. The actor moves to the lower left of the mouse.

  • SocksSocks London, UK.Posts: 12,822Member
    edited February 2015

    @Meeped said:
    Thanks, it works, but there's a small problem. When I press down, the mouse arrow is off the image and it moves the actor, but they're out of sync together.

    Well at least they are out of sync together, better than being separately out of sync. :wink:

    @Meeped said:
    The actor moves to the lower left of the mouse.

    I'd guess the issue is with the way you have your code set up ?

  • MeepedMeeped Posts: 31Member

    @Socks said:
    I'd guess the issue is with the way you have your code set up ?

    It's exactly how you have it, I'm wondering if the scene is too small, because I am working with a small scene.

  • MeepedMeeped Posts: 31Member

    @Meeped said:
    It's exactly how you have it, I'm wondering if the scene is too small, because I am working with a small scene.

    No, that's not it. I have it exactly how you have it. It looks like this:

                           (mouse cursor here)
    

    ........
    ........(actor here)
    ........

  • SocksSocks London, UK.Posts: 12,822Member

    @Meeped said:
    It's exactly how you have it . .

    Could you take a screenshot of your rules.

  • MeepedMeeped Posts: 31Member
    edited February 2015

    @Socks said:
    Could you take a screenshot of your rules.

    Unfortunately Alt+Print Screen doesn't work on my dinosaur of a keyboard.

    Here's what the rule states

    When touch is pressed:

    Constrain attribute: self.position.x to: game.Mouse.position.x
    Constrain attribute: self.position.y to: game.Mouse.position.y

  • SocksSocks London, UK.Posts: 12,822Member

    @Meeped said:
    Unfortunately Alt+Print Screen doesn't work on my dinosaur of a keyboard.

    The rules should work fine, but it would be good to see your actual rules just to check that there are no mistakes you've overlooked.

    If your keyboard doesn't work then use the Snipping Tool.

  • MeepedMeeped Posts: 31Member
    edited February 2015

    @Socks said:
    Could you take a screenshot of your rules.

    [invalid image format removed]

  • MeepedMeeped Posts: 31Member

    Just copy and paste that. sorry I didn't mean to make it so long

  • SocksSocks London, UK.Posts: 12,822Member
    edited February 2015

    :#

    If you have an iPad or an iPhone or an Android phone - maybe you could take a picture of your rules and upload that instead.

  • MeepedMeeped Posts: 31Member

    @Socks said:
    :#

    If you have an iPad or an iPhone or an Android phone - maybe you could take a picture of your rules and upload that instead.

    didnt work for you?

  • SocksSocks London, UK.Posts: 12,822Member
    edited February 2015

    @Meeped said:
    didnt work for you?

    Yeah, it worked in the end, but two of the central pieces of information are hidden so I'll just have to go with what you've said above, I guess that something must be wrong elsewhere in your project.

  • MeepedMeeped Posts: 31Member

    alright, I'll keep looking around.

  • RainbrosRainbros Posts: 124Member

    I recommend, instead of simply constraining the actor's position to the mouse position, this logic:

    when mouse button is down,

    constrain self.motion.linearvelocity x to: (mouse.position.x - self.position.x) * constant
    constrain self.motion.linearvelocity y to: (mouse.position.y - self.position.y) * constant

    where "constant" is a number of your choosing that will determine the amount of lag on the drag. Somewhere around 20-30 should do the trick.

    This method is a lot smoother, and I imagine that for a dodging type game, this is better because it prevents "teleporting" your actor to avoid enemies.

  • MeepedMeeped Posts: 31Member

    @Rainbros said:
    I recommend, instead of simply constraining the actor's position to the mouse position, this logic:

    when mouse button is down,

    constrain self.motion.linearvelocity x to: (mouse.position.x - self.position.x) * constant
    constrain self.motion.linearvelocity y to: (mouse.position.y - self.position.y) * constant

    where "constant" is a number of your choosing that will determine the amount of lag on the drag. Somewhere around 20-30 should do the trick.

    This method is a lot smoother, and I imagine that for a dodging type game, this is better because it prevents "teleporting" your actor to avoid enemies.

    Actually, it did quite the opposite. The actor I wanted to move immediately flew to the lower left and didn't stop.

  • gyroscopegyroscope I am here.Posts: 6,577Member, Sous Chef, PRO

    Hi @Meeped try the following in your actor to be dragged, but first make two self attributes (real) in the prototype actor before putting on the screen - offsetx and offsety, and a third as a boolean called drag.

    Rule: When touch is pressed
    Change Attribute self.offsetx to game.Mouse.Position.X-self.Position.X
    Change Attribute self.offsety to game.Mouse.Position.Y-self.Position.Y
    Change self.drag to true
    
    Rule: When self.drag is true
    Constrain Attribute self.Position.X to game.Mouse.Position.X-self.offsetx
    Constrain Attribute self.Position.Y to game.Mouse.Position.Y-self.offsety
    

    (You can replace Mouse with Touch if it's for a device, if you prefer).

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • MeepedMeeped Posts: 31Member

    @gyroscope works perfectly! thank you so much! thanks to everyone else who helped as well.

    Just a quick question, though. I do want to replace Mouse with Touch, can I do both?

    Touch is more important, but there are Touch 1, Touch 2, Touch 3 (and so forth) which do I use?

  • gyroscopegyroscope I am here.Posts: 6,577Member, Sous Chef, PRO

    Touch 1 will do the trick.

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • MeepedMeeped Posts: 31Member

    @gyroscope also, I just found that once you press the actor, you can't let go.

  • gyroscopegyroscope I am here.Posts: 6,577Member, Sous Chef, PRO

    Oh, not sure why that's happening... it should work without the following but try adding another Rule:

    When Touch is released
    Change self.drag to false
    

    That should cure it.

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • MeepedMeeped Posts: 31Member

    Thank you very much

  • karenvhkarenvh Posts: 1Member

    Hi! I have a related question and you folks seem to be a lot more experienced than I am (I've been at it only a couple of weeks, but my game is nearly done). I have multiple actors that can each be dragged. They're tiles that can be moved around and rearranged like puzzle pieces. If the mouse button is inside the actor and down, self.dragging=true, and then the actor moves with the mouse. So far so good but if two actors collide quickly, one can end up on top of the other, and then they're both under the mouse position and so both go to self.dragging=true and they get dragged around together. I want it to be that only the one you were dragging first gets dragged and the others don't stick to it. But so far I haven't been able to make them bounce sufficiently on collision, or anything like that, to stop them from sometimes ending up one on top of another and then sticking together. I'd be grateful for any suggestions!

  • I have a question. When I did the first player in my game, it worked perfectly. When I add my second player with Touch 2, it went bouncing around the screen after I let go Touch 1 in the game. Here is my code.

Sign In or Register to comment.