A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: mouse coords -> move MC

  1. #1
    Junior Member
    Join Date
    Sep 2000
    Posts
    13
    Hi,

    A very simple problem:
    I've got this background (the map of a city). I want it to move accordingly to the position of the mouse : if the mouse is 10 pixels from the top of the movie, I want the background to move upwards. (Just like The Sims, or any RPG computer game.)

    So this is what I've done:
    on the first level, I've put a MC called 'fond'. This contains the map of the city and this script :

    <PRE>onClipEvent (mouseMove) {
    xpos = _root._xmouse;
    ypos = _root._ymouse;
    }
    onClipEvent (load) {
    while (ypos<=2) {
    setProperty (_root.fond, _y, Number(getProperty(_root.fond, _y))+10);
    }
    }</PRE>

    This does not work : i guess i get an infinite loop, because the flash player goes unresponsive.
    Any ideas ??
    Thanks for your help!

  2. #2
    Try this:

    onClipEvent (enterFrame) {
    &nbsp;&nbsp;&nbsp;&nbsp;if (_root._ymouse < 10) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;th is._y += 10;
    &nbsp;&nbsp;&nbsp;&nbsp;}
    }

    it should work, but make sure the movieclip "fond" does not stop, it has to loop, or else there is no frame being entered after the stop.


  3. #3
    Hack
    Join Date
    Mar 2000
    Location
    Madison, WI
    Posts
    1,753
    The deal with while loops in flash is that they don't update the screen in between each loop.

    What you want to do instead of on (load) is maybe on (enterFrame) and then just use an if statement instead of a while. Then on every frame if the mouse is near the edge it will move fond.

    Pump up your frame rate for smooth movement.

  4. #4
    Junior Member
    Join Date
    Sep 2000
    Posts
    13

    It worked out fine !

    OK ! It works fine. thanks a lot!
    For those interested, the final code looks like this :
    <PRE>onClipEvent (enterFrame) {
    // go up left
    if ((_root._ymouse<10 & _root._xmouse<150) || (_root._ymouse<150 & _root._xmouse<10)) {
    this._y += 10;
    this._x += 10;
    }
    // go up
    if ((_root._ymouse<10) & ((_root._xmouse>150) & (_root._xmouse<650))) {
    this._y += 10;
    }
    // go up right
    if ((_root._ymouse<10 & _root._xmouse>650) || (_root._ymouse<150 & _root._xmouse>650)) {
    this._y += 10;
    this._x -= 10;
    }
    // go right
    if ((_root._xmouse>790) & ((_root._ymouse>150) & (_root._ymouse<450))) {
    this._x -= 10;
    }
    // go down right
    if ((_root._ymouse>450 & _root._xmouse>790) || (_root._ymouse>450 & _root._xmouse>650)) {
    this._y -= 10;
    this._x -= 10;
    }
    // go down
    if ((_root._ymouse>590) & ((_root._xmouse>150) & (_root._xmouse<650))) {
    this._y -= 10;
    }
    // go down left
    if ((_root._ymouse>450 & _root._xmouse<10) || (_root._ymouse>590 & _root._xmouse<150)) {
    this._y -= 10;
    this._x += 10;
    }
    // go left
    if ((_root._xmouse<10) & ((_root._ymouse>150) & (_root._ymouse<450))) {
    this._x += 10;
    }
    }</PRE>
    Of course this is very slow ( one FPS because of large graphics etc), so now the question is :
    <B>How to toggle to low quality while scrolling ?</B>
    I tried this :
    <PRE> // optimizing speed
    if ((_root._xmouse>10 & _root._ymouse>10) & (_root._xmouse<790 & _root._ymouse<590)) {
    _quality = HIGH;
    } else {
    _quality = LOW;
    }</PRE>
    But it flashes on and off high/low quality ://

    ciao.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center