To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-13-2004, 09:32 PM   #1
JBOMBER
Member
 
Join Date: Oct 2002
Location: Los Angeles
Posts: 77
movie clip x and y position relative to mouse [link included]

hi..

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 :
http://www.battlelounge.com/reticle.htm

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
__________________
-JB
JBOMBER is offline   Reply With Quote
Old 10-14-2004, 04:26 AM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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:

code:

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.
Attached Files
File Type: fla crosshair.fla (17.5 KB, 1379 views)
__________________

Last edited by jbum; 10-14-2004 at 04:33 AM.
jbum is offline   Reply With Quote
Old 10-14-2004, 10:42 PM   #3
JBOMBER
Member
 
Join Date: Oct 2002
Location: Los Angeles
Posts: 77
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
__________________
-JB
JBOMBER is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:22 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.