2 Replies Latest reply on Jun 5, 2006 7:01 AM by Newsgroup_User

    2D Rotation in the users hands...

    adeking
      Hi,

      Hope someone can help. I am a teacher and am looking to create an educational game in Flash. The game is basically Dominoes. I can use actionscript to give the user basic drag and drop functionality, so that different domino peices can be matched, but I need to incorparate functionality so that they can rotate the peices 90 degrees, as you would in a normal game of dominoes.

      Any Ideas?

      Thanks for your time
        • 1. Re: 2D Rotation in the users hands...
          Tolk Level 1
          you could use something like (written in english not as):

          domino.onClick = function
          if key "ctrl" is down
          rotate domino movieclip
          else
          start drag

          domino.onRelease = function
          stop drag


          so if they had ctrl down when they click the domino it rotates it, if they don't it drags it.
          action script for rotating symbols is:

          instancename._rotate = value;
          • 2. Re: 2D Rotation in the users hands...
            Level 7
            >> I can use actionscript to give the user basic drag and
            >> drop functionality, so that different domino peices can
            >> be matched, but I need to incorparate functionality so
            >> that they can rotate the peices 90 degrees, as you
            >> would in a normal game of dominoes.

            > domino.onClick = function
            > if key "ctrl" is down
            > rotate domino movieclip
            > else
            > start drag
            > domino.onRelease = function
            > stop drag

            While this may be helpful in a conceptual sense, I suspect adeking could
            use a few suggestions based on actual ActionScript syntax. To that end,
            here's a crash course in approaching ActionScript. It helps greatly to know
            how the documentation is organized; specifically, how the ActionScript 2.0
            Language Reference is organized.

            Just about everything in ActionScript can be referred to as an object.
            Objects are "things" you can manipulate using code. Things an object can do
            are called methods; things an object can react to are called events;
            characteristics an object has are called properties. One or more of these
            three categories appears in something called "class entries" for the object
            in question. Classes are a kind of "blue print" that define how a
            particular object behaves. Movie clips are defined by the MovieClip class;
            dynamic and input text fields by the TextField class; and so on.

            If you look up the MovieClip class entry of the ActionScript 2.0
            Language Reference, for example, you'll find that all movie clips have a
            MovieClip._rotation property (characteristic). So all you have to do is
            give each of your movie clips a unique instance name -- each movie clip
            symbol is an instance of the MovieClip class, so each instance needs an
            instance name -- and then you can tell each clip what to do.

            e.g.
            domino1._rotation = 45;
            domino1._rotation = 90;

            The above sets the MovieClip._rotation property of a domino with the
            instance name domino1 to 45, then to 90 degress.

            If you've done the drag and drop stuff, you're already familiar with
            events and methods.

            domino1.onPress = function() {
            this.startDrag();
            }
            domino1.onRelease = function() {
            this.stopDrag();
            }

            In the above, a function is assigned to the MovieClip.onPress event of
            domino1. The function simply invokes the MovieClip.startDrag() method on
            the MovieClip instance in question. The MovieClip.onRelease event of this
            clip also has a function assigned to it, which invokes the
            MovieClip.stopDrag() method.

            I find that if you think about the Language Reference in this manner,
            you'll be in for a pleasant eye-opener. Each object effectively has an
            Owner's Manual -- which should help you sift through the documentation much
            more quickly. No more hunting and pecking.

            Tolk's suggestion isn't a bad one. You may, indeed, want to drag or not
            drag based on whether the Ctrl key (or any key, such as R [for "rotate"]) is
            held or not. Since you're dealing with keys, then, you'll want to look up
            the Key class.

            domino1.onPress = function() {
            if (Key.isDown(Key.CONTROL)) {
            this._rotation = 90;
            } else {
            this.startDrag();
            }
            }
            domino1.onRelease = function() {
            this.stopDrag();
            }

            Of course, in the above example, the domino will only rotate to 90
            degrees once. There's no going back. You could have it *incremenet* by 90
            every time. That may work for you ...

            domino1.onPress = function() {
            if (Key.isDown(Key.CONTROL)) {
            this._rotation += 90;
            } else {
            this.startDrag();
            }
            }
            domino1.onRelease = function() {
            this.stopDrag();
            }

            .... or have your code check first if it's 90, then either go to 90 or to 0.

            domino1.onPress = function() {
            if (Key.isDown(Key.CONTROL)) {
            if (this._rotation == 90) {
            this._rotation = 0;
            } else {
            this._rotation = 90;
            }
            } else {
            this.startDrag();
            }
            }
            domino1.onRelease = function() {
            this.stopDrag();
            }

            Note a few "gotchas" ... note the difference between == (comparison) and
            = (assignment). Note, also the += (increment) operator. In fact, just look
            up "operators" in the ActionScript 2.0 Language Reference to get a feel for
            what each does.

            You may want to check out these articles for further detail.

            http://www.quip.net/blog/2006/flash/actionscript-20/ojects-building-blocks
            http://www.quip.net/blog/2006/flash/tackling-the-actionscript-20-language-reference
            http://www.quip.net/blog/2006/flash/debugging-actionscript


            David
            stiller (at) quip (dot) net
            Dev essays: http://www.quip.net/blog/
            "Luck is the residue of good design."