Making characters / objects follow the cursor...

Started by Duckbutcher, Tue 15/04/2008 18:01:34

Previous topic - Next topic

Duckbutcher

...how would I go about this? I want a character / object  (whichever is simpler) to follow the cursor around the game screen.

Any ideas?

Snake

#1
If this is going to be used throughout the game, then a character would be the better idea. If it's just for one room, an object is the better choice unless you are restricted by how many objects you will be using in that one room.

Either way, if I were doing it myself, I'd probably use a character.

This is untested, but I'd guess the code would be similar to this (in it's simpler version):
Code: ags

//global script
//function repeatedly_execute_always();
//let's say the thing that is following the cursor is character 5...
character[5].x=mouse.x;
character[5].y=mouse.y;


Then you can fool around with the coordinates and see what suits you the best with what you're using it for, like this:
Code: ags

character[5].x=mouse.x+50;
character[5].y=mouse.y+10;


I'm not sure about this, since I haven't done it myself yet, but you might have to set the mouse boundries so that you don't get an error messege saying that the character went out of the room's coords if he/she/it is a few pixels away from the mouse when the mouse is at it's limits. You might have to shrink it a little.


I hope this helps and that I'm on the right track ;)

\\--EDIT--\\
Hang on, forget the mouse boundries idea.
Try something like this if it gives you a problem:
Code: ags

//if your character is to the RIGHT of the cursor, say, 10 pixels...
if (mouse.x>=318) character[5].x=mouse.x-10; //assuming that you're game is 320x200 or 240...


This of course isn't exact (according to what you'll be doing), you'll just have to fool with it.


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Duckbutcher

#2
Cheers bud, I'll tackle it and let you know if it throws up any probs!

Edit: It works fine. Thanks a lot for your help!

Snake

Nice, I'm glad I was of some help.

I think this was the first time I helped somebody on this forum - it feels great :)


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Duckbutcher

Right, just one last thing to push it a bit further...sorry! This is more of an afterthought which would make a certain part of my game pretty cool...

Say I have a  hotspot which covers the screen. Clicking on this hotspot will 'switch on' an object/character. But I want this object / character to appear at the exact position underneath the cursor, and then STAY at that fixed position while the cursor goes off and does something else.

How would I go about making this object/character appear at the exact position that the mouse clicks and then stay there?

Snake

Cool!

All you have to do is this:

Code: ags

//code - blah blah
object[#].x=mouse.x;
object[#].y=mouse.y+10;//or whatever you want.
object[#].visible=true;


Like what I said before, you need to play with it (haha) but you'll get it just right afterwords - the position of the object I mean...

NOTE:
The code that I gave you will NOT be in the repeatedly execute. Just in the hotspot's interaction - which ever you choose (ie; talk, walk, interact, look..). This way it only happens at the time of the click, not all the time, hence, the object being turned on at the x,y coordinates of the mouse and then staying there.

Let me know if this works (it should),


--Snake
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Duckbutcher

Hmm, it throws up an error -  it says that '.x' is not a public member of 'object'.

I put it into script under "any click on hotspot"

Any ideas? I'm still a bit shaky when it comes to understanding script, so I'm a little stumped.


Duckbutcher


SMF spam blocked by CleanTalk