Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-28-2011, 03:34 PM   #1
j pearce
Registered User
 
Join Date: Oct 2010
Posts: 48
Default edit Point?

how can I change a Point variable without using new Point?
In the constructor I have this to establish the variable:
ActionScript Code:
public var target:Point = new Point(0,0);
but in the looped function I want the target to equal mouseX,mouseY in real time i.e.
ActionScript Code:
target.x = mouseX; target.y = mouseY;
something like that (it must just change the existing point, not create a new one).
thanks
j pearce is offline   Reply With Quote
Old 08-28-2011, 05:34 PM   #2
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

Hi j,

Yes, that's how you do it. See: http://help.adobe.com/en_US/FlashPla...ropertySummary

I hope this helps!
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 08-28-2011, 05:54 PM   #3
j pearce
Registered User
 
Join Date: Oct 2010
Posts: 48
Default

Quote:
Originally Posted by matbury View Post
Hi j,

Yes, that's how you do it. See:

I hope this helps!
Hi thanks yeah that did help Think the reason it didn't work was that one of my other calculations was messing up. Basically what i'm doing is making an MC rotate to face the mouse and move towards it at a certain speed - works fine in AS2 but cant get it to work in AS3! Is there any difference between trig functions in AS2 & AS3?

Here's what I have:

ActionScript Code:
package {     import flash.display.*;     import flash.events.*;     import flash.geom.*;     public class EnemySub extends MovieClip {         public var target:Point = new Point(550,400);         public var speed:Number = 20;         public var xDiff:Number;         public var yDiff:Number;         public var hyp:Number;         public var Angle1:Number;         public var Angle2:Number;         public var ShipSpeedMS:Number = 15;         public var FinalShipSpeed:Number;         public var DivisionFactor:Number = 30;         public var hypDPS:Number;         public var xDPS:Number;         public var yDPS:Number;         public var hypDPSPercentage:Number;         public function EnemySub() {             x = 200;             y = 200;         }         public function EnemySubMove():void {             //Target             target.x = mouseX;             target.y = mouseY;             //Rotation             xDiff = this.x - target.x;             yDiff = this.y - target.y;             hyp = Math.sqrt((xDiff * xDiff)+(yDiff * yDiff));             Angle1 = (Math.atan2((target.y-this.y),(target.x-this.x)));             Angle2 = Angle1*(180/Math.PI);             this.graphic.rotation = Angle2;             //ETA & Speed             FinalShipSpeed = ShipSpeedMS/DivisionFactor;             hypDPS = (FinalShipSpeed/hyp)*100;             hypDPSPercentage = (FinalShipSpeed/hyp)*100;             xDPS = ((xDiff/100)*hypDPSPercentage);             yDPS = ((yDiff/100)*hypDPSPercentage);             //Movement             this.x -= xDPS;             this.y -= yDPS;         }     } }
Just seems to go where ever it likes because I think one of the trig functions is wrong... Any ideas anyone? BTW, if I just set the point coords it works fine, but by changing them whilst it moves to the mouse x & y it screws up...
Thanks.
j pearce is offline   Reply With Quote
Old 08-28-2011, 06:31 PM   #4
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

Maths stays the same no matter what language you're using!

Check out the docs on Point again, there's some interesting functions for calculating distances, etc.

On your question, here's Keith Peter's approach (Copied and pasted from this book: http://www.apress.com/9781590597910). Arrow is a Sprite in the FLA library - you'll have to draw it yourself:
ActionScript Code:
package {     import flash.display.Sprite;     import flash.events.Event;         public class RotateToMouse extends Sprite {                 private var arrow:Arrow;                 public function RotateToMouse() {             init();         }                 private function init():void {             arrow = new Arrow();             addChild(arrow);             arrow.x = stage.stageWidth / 2;             arrow.y = stage.stageHeight / 2;             addEventListener(Event.ENTER_FRAME, onEnterFrame);         }                 public function onEnterFrame(event:Event):void {             var dx:Number = mouseX - arrow.x;             var dy:Number = mouseY - arrow.y;             var radians:Number = Math.atan2(dy, dx);             arrow.rotation = radians * 180 / Math.PI;         }     } }

BTW, I love Keith Peter's book. It covers all the basics of dynamic 2D and 3D interactive animation and is project based so you get plenty of hands-on learning opportunities.
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 08-28-2011, 07:36 PM   #5
j pearce
Registered User
 
Join Date: Oct 2010
Posts: 48
Default

Thanks mate I really appreciate this
j pearce is offline   Reply With Quote
Old 08-28-2011, 08:32 PM   #6
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

You're welcome! Glad to help.
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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. The time now is 04:03 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.