movie clip x and y position relative to mouse [link included]
i want to create a shooting gallery type game but instead of having the "reticle" for my gun lock to the cursor position i would like to have it relative to a box at the bottom of my movie.
here is an example of how it would look :
right now when you click on the flash piece you can control the reticle with the arrow keys (something i want)
can someone point me in the right direction as to how i would make the reticle relative to mouse position within that box at the bottom (the little red dot would lock the cursor within that clip)
ive done a board search and worked a few tutorials and most of em deal with replacing the cursor, telling a MC to lock to the mouse cursor and locating the mouse position.
Anyone know of a tut or thread with something similar to what im describing? Later ill try and add some inertia and perhaps a gun that follows the reticleMC angle.
thanks for reading all that
I'm not sure what a reticule is, but I'm assuming you mean a little map that behaves like the larger movie, with a moving dot that is a tiny version of the crosshair. What I sometimes call "radar" in my games.
The enclosed movie does this.
Here is the script:
SW = Stage.width;
SH = Stage.height;
crosshair_mc.onEnterFrame = function()
// use mouse position to move crosshair
this._x = _root._xmouse;
this._y = _root._ymouse;
// convert mouse position to normalized ratio
var rx = _root._xmouse/SW; // x mouse ratio (0-1)
var ry = _root._ymouse/SH; // y mouse ratio (0-1)
// use ratio to change scale of movement - moving dot by reticule._width and reticule._height
reticule_mc.dot_mc._x = rx * reticule_mc._width;
reticule_mc.dot_mc._y = ry * reticule_mc._height;
In the script, crosshair_mc is the crosshair movieclip that follows the mouse.
reticule_mc is the movieclip that contains both the rectangle at the bottom, and the dot.
reticule_mc.dot_mc is the dot inside the reticule. When it's _x position is zero, it is on the left edge of the rectangle, and when it's _x position is reticule_mc._width, it is on the right side.
Basically this is a scaling operation, meaning it involves multiplication.
The trick to any kind of scaling mathematics is converting the variable that changes (the mouse position, in this case) to a normalized ratio - that is a variable whose value goes from zero to one. Once you get it normalized, it's easy to convert to any other scale - you just multiply it by the dimension you want to change it to.
Take the _xmouse value. This initially goes from 0 to Stage.width (the width of the stage). To change it to a normalized ratio, I divide by Stage.width.
rx = _root._xmouse / Stage.width;
0/Stage.width == 0, and Stage.width/Stage.width == 1,
so now we have our ratio.
Then I multiply the ratio rx by reticule._width and now the value goes from 0 to reticule._width, which is exactly how I want the dot to move.
reticule_mc.dot._x = rx*reticule_mc._width;
These two operations can be combined,
eliminating the rx variable:
reticule_mc.dot_mc._x = _root._xmouse*reticule_mc._width / Stage.width;
Hope this makes sense.
Last edited by jbum; 10-14-2004 at 04:33 AM.
ya know....certain members of this board never stop amazing me.
jbum being one of them. senocular, gparis, subway...so many others...Incredible skills, still helpin out the little guy.
im no script kiddie so thank you jbum...i promise not to just straight rip your code..but to go through it and try and understand whats going on. Try and make it worth your time by posting.... by actually learning from it and applying my version of what i have learned. This isnt something i ever plan to make money or anything off of..just something i wanted to build for fun.
so..thanks again..its appreciated
Click Here to Expand Forum to Full Width