10 Replies Latest reply: Aug 9, 2012 1:46 AM by oanhyen RSS

    Moving an object on the "floor"?

    oanhyen Community Member

      Hi. I want to do something similar in this nickjr game but I'm not sure how to move an object with the mouse only on the floor of a room instead of the whole space. Any ideas?

       

      children game:http://www.nickjr.com/kids-games/blues-clues-ghost-hunt.html

       

      thanks,

      Kim

        • 1. Re: Moving an object on the "floor"?
          Ned Murphy MVP

          You need to detect collisions with boundary objects and use them to prevent movement beyond those boundaries.

          • 2. Re: Moving an object on the "floor"?
            oanhyen Community Member

            okay thank you but I want to know if I can somehow attach the object moving around the room to the floor so that it doesn't "fly" when I move the mouse up. Sorry I'm still a beginner in flash so I'm not sure what I'm meant to search up.

             

            thanks.

            • 3. Re: Moving an object on the "floor"?
              Ned Murphy MVP

              I am not sure what you mean, but attaching a moveable object to another will not do anything to prevent its movement.  You need to create the controls that limit the movement.

              • 4. Re: Moving an object on the "floor"?
                oanhyen Community Member

                Sorry for not making an sense, but in that game, the dog is following the mouse and I've already managed to do that but what I can't do or don't know what to research is how to make the dog/object stay on the floor only when I move the mouse. For example, when I move my mouse to the left, the object would follow but it would stay on the floor. 

                 

                thank you.

                • 5. Re: Moving an object on the "floor"?
                  Ned Murphy MVP

                  Where is it when it is not on the floor?  I think you might be confusing a visual effect with a physical one... In Flash it is all visual.

                  • 6. Re: Moving an object on the "floor"?
                    oanhyen Community Member

                    oh! I meant for my game that I'm creating now, I managed to make an object follow the mouse but I don't know how to make it move just on the floor(like in that game, the dog is following the mouse but it's stays on the floor).

                     

                    If this way doesn't work, is there a way to make an object move only horizontally when you move the mouse? So like if the object is positioned at the bottom of the screen and if I move the mouse, the object will follow but it will only move along the botton of the screen.

                    • 7. Re: Moving an object on the "floor"?
                      Ned Murphy MVP

                      Your response brings me back to my first response.  You need to use collision detection in order to keep the object wherever you want it to be limited to.

                       

                      If you want the object to only move horizontally, then you only change its _x property relative to the mouse's x position - and do nothing to change its _y property

                       

                      If you have some codeyou are currently using, it would be easier to answer based on that than anything else.

                      • 8. Re: Moving an object on the "floor"?
                        oanhyen Community Member

                        Yeah I finally realised that I have to use collision detection...

                         

                        Okay I got this code, which is when you use the keyboard keys to move an object to collide into a wall so how can I change it so the object moves using mouse instead of keyboard?

                         

                        onClipEvent(enterFrame) {

                                        speed=5;

                                        if(Key.isDown(Key.RIGHT)) {

                                                        _x +=speed;

                                                        if(this.hitTest(_root.wall)){

                                                                        _x -=speed;

                                                        }

                                        } else {

                                                        if(Key.isDown(Key.LEFT)){

                                                                        _x -=speed;

                                                        if(this.hitTest(_root.wall)){

                                                                        _x +=speed;

                                                        }

                                                        }

                                        }

                                        if(Key.isDown(Key.UP)) {

                                                        _y -=speed;

                                                        if(this.hitTest(_root.wall)){

                                                                        _y +=speed;

                                                        }

                                        } else {

                                                        if(Key.isDown(Key.DOWN)){

                                                                        _y +=speed;

                                                        if(this.hitTest(_root.wall)){

                                                                        _y -=speed;

                                                        }

                                                        }

                                        }

                                        if(_x>550){

                                                        _x = 1;

                                        } else {

                                                        if(_x<1){

                                                                        _x = 550;

                                                        }

                                        }

                                        if(_y>400){

                                                        _y = 1;

                                        } else {

                                                        if(_y<1){

                                                                        _y = 400;

                                                        }

                                        }

                        }

                        Thanks.

                        • 9. Re: Moving an object on the "floor"?
                          oanhyen Community Member

                          Or what can i do to this code so that when the ball hits a wall (using mouse not keyboard) it doesn't go through the wall?

                           

                          onEnterFrame = function()

                          {

                             if(wall5.hitTest(ball))

                             {

                                trace("the ball is positioned over wall");

                           

                             }

                          }

                           

                          oh I have this code for the ball mc

                           

                          onClipEvent(enterFrame) {

                          x=_root._xmouse

                          y=_root._ymouse

                          _x+=(x-_x)

                          _y+=(y-_y)

                          }

                          • 10. Re: Moving an object on the "floor"?
                            oanhyen Community Member

                            Can someone please help me?

                            I've been trying a lot of tutorials. This is the closest to what I want.

                             

                            target_MC = ["circle", "wall2"];

                            speed = _root.speed=Math.round(Math.random()*5)+1;

                            this.onEnterFrame = function() {

                                      hitTester("circle");

                            };

                            hitTester = function (cName) {

                                      numCircles = target_MC.length;

                                      circleA = target_MC[i];

                                      for (i=0; i<numCircles; i++) {

                                                circleA = target_MC[i];

                                                for (j=i+1; j<numCircles; j++) {

                                                          circleB = target_MC[j];

                                                          if (eval(circleA).hitTest(eval(circleB))) {

                                                                    collidOver(eval(circleA), eval(circleB));

                                                          }

                                                }

                                      }

                            };

                            collidOver = function (objectA, objectB) {

                                      m_x = eval(objectA)._x;

                                      m_y = eval(objectA)._y;

                                      o_x = eval(objectB)._x;

                                      o_y = eval(objectB)._y;

                                      if (m_x=o_x) {

                                                eval(objectA)._x = 0;

                                      }

                                      if (m_y=o_y) {

                                                eval(objectA)._y = 0;

                                      }

                            };

                             

                            I used the collision with mouse follow/click file from here http://www.kirupa.com/developer/actionscript/multiple_collision3.htm because it's similar to what I want but the balls move when the main ball collides with them. In the code above, I managed to make the wall remain still when the ball collides with it but when it collides the ball returns to the position (0,0), which is in this part of the code:

                              if (m_x=o_x) {

                              eval(objectA)._x = 0;

                              }

                              if (m_y=o_y) {

                              eval(objectA)._y = 0;

                              }

                             

                            But I don't know what to type there so the ball stays next to the wall instead of going to the position (0,0).

                             

                            I would love to get this part done cause I've been stuck for almost a week now.

                             

                            Thanks!