How to move an actor by dragging it
Meeped
Member Posts: 31
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
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.
Well at least they are out of sync together, better than being separately out of sync.
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.
No, that's not it. I have it exactly how you have it. It looks like this:
........
........(actor here)
........
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
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.
[invalid image format removed]
Just copy and paste that. sorry I didn't mean to make it so long
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?
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.
alright, I'll keep looking around.
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.
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.
(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
@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?
Touch 1 will do the trick.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
@gyroscope also, I just found that once you press the actor, you can't let go.
Oh, not sure why that's happening... it should work without the following but try adding another Rule:
That should cure it.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
Thank you very much
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.