Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums Angular & Speed Based Motion Code Here.
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 12-20-2007, 11:29 AM #1 Bracer Bracer   Join Date: Jul 2007 Posts: 113 Angular & Speed Based Motion Code Here. I've created this function to emulate a directional based motion establisher in Actionscript from Game Maker instead of your usual Hspeed & Vspeed. //////////////////////////////////////////////////////////////////////////// /* Bracer, this function allows you to emulate Game Maker's version of the "Move Free" Function. */ function MoveFree (ObjectToMove, Speed, Direction) { //Direction = Angle //Speed = Hypotenuse Direction = - Direction; //Actually, this inverse is NOT suppose to be, //however since Game Maker's rotation for some reason is counter clockwise based, //I might as well match it for consistency. //We first retrieve the next ._x position of your object. //Cosine(Angle) = Adjacent/Hypotenuse //Cosine(Direction) = _x position / Speed ObjectToMove._x += Math.cos(Math.PI/180 * Direction) * Speed; //Now time to handle the ._y position of your object. //Sine(Angle) = Opposite / Hypotenuse //Sine(Direction) = ._y / Speed ObjectToMove._y += Math.sin(Math.PI/180 * Direction) * Speed; //The extra stupid "Math.PI/180 *" is due to the stupid fact that //flash's trigonometry functions are based on radian, WTF right ? } //////////////////////////////////////////////////////////////////////////// Ball_mc.onEnterFrame = function() { MoveFree(this,5,45); } Hope you find this useful. __________________ Available Freelancer
 01-07-2008, 04:59 PM #2 Afromonkey0 Registered User   Join Date: Jan 2008 Posts: 6 Very useful thanks I'm a GMLer trying out actionscript too, and AS has NO DAMN FUNCTIONS! how can anyone build games with a language with no distance calculating function? No speed variable? I had a look online to find GML functions for actionscript and found that thread of yours that got stopped on the GML forum. That was pretty harsh of them. So i kept looking and found this one So anyway i'm trying to set up a library of GML functions so that you can call them in actionscript, and this is pretty helpful. I'm making the ones i use most first, point_direction, point_distance, lengthdir_x, lengthdir_y, motion_add etc. So if we both keep making AS GML functions, we can share them and end up with a complete GML library! I'll send you my ones when i'm done making them.
 01-07-2008, 05:46 PM #3 ASWC Super Moderator     Join Date: Dec 2007 Location: Greenville, SC Posts: 6,528 Me I'm more interested about collision detection. Moving stuff around is pretty straight forward to me but for some reason, I find it harder to get a good collision detection in 2D Flash than in 3D gaming with C++ and OpenGL. Obviously my MCs have from time to time a tendency to pass throught other MCs, get caught by other MCs, or bounce back with the wrong angle. By tweaking my code and trying and trying I was able to low these tendencies to about 1 or 2 % of the time but still it's not good enough. Since these bad behaviors increase as the speed increase it was quite easy to find what was wrong but still the perfect solution did not appear to me!
 01-07-2008, 07:14 PM #4 Bracer Bracer   Join Date: Jul 2007 Posts: 113 Fantastic Afromonkey0! I love the idea of doing something for the community. Let me know what codes you have come up with in time You can catch up on my development on "Interactive Land". This blog is not entitled exclusively to Game Maker though, it's more toward interactivity in general __________________ Available Freelancer
 01-08-2008, 03:06 PM #5 Afromonkey0 Registered User   Join Date: Jan 2008 Posts: 6 I'm having some problems replicating GML functions actually. I made the functions global, so any object can use them, but i can't find a way for a script to know which object is calling it. It's annoying to have to pass the name as a parameter, i guess that's what i'll do though.
 01-08-2008, 03:27 PM #6 Afromonkey0 Registered User   Join Date: Jan 2008 Posts: 6 I've made a bunch of functions, passing the name as a variable with 'this', but all the ones which involve variables in the calling object don't work. Here's the code for the function declarations Code: ```stop(); _global.GMLinit = function(object) { var object._speed; var object._direction; var object._hspeed; var object._vspeed; }; _global.GMLstep = function(object) { setvector(object); setsplit(object); object._x += object._hspeed; object._y += object._vspeed; }; _global.point_distance = function(x1, y1, x2, y2) { var dx = x2-x1; var dy = y2-y1; return Math.sqrt(dx*dx+dy*dy); }; _global.point_direction = function(x1, y1, x2, y2) { var dx = x2-x1; var dy = y2-y1; // value in radians; multiply by (180/Math.PI) for degrees return ((Math.atan2(dy, dx))*(180/math.PI)); }; _global.lengthdir_x = function(len, dir) { return (Math.cos(Math.PI/180*dir)*len); }; _global.lengthdir_y = function(len, dir) { return (Math.sin(Math.PI/180*dir)*len); }; _global.setvector = function(object) { // sets direction and speed from hspeed and vpeed object._direction = point_direction(0, 0, object._hspeed, object._vspeed); object._speed = point_distance(0, 0, object._hspeed, object._vspeed); }; _global.setsplit = function(object) { // sets hspeed and vspeed from speed and direction object._hspeed = lengthdir_x(object._speed, object._direction); object._vspeed = lengthdir_y(object._speed, object._direction); }; _global.motion_add = function(object, speed, dir) { object._hspeed += lengthdir_x(speed, dir); object._vspeed += lengthdir_y(speed, dir); setvector(object); };``` It's probably something stupid, since this is my first ever actionscript code.
 01-08-2008, 03:28 PM #7 Bracer Bracer   Join Date: Jul 2007 Posts: 113 That's what I do too, pass "this" as an argument. Ha Ha Ha __________________ Available Freelancer

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

 Similar Threads Thread Thread Starter Forum Replies Last Post jasonoda ActionScript 1.0 (and below) 10 01-06-2011 01:52 PM hmc ActionScript 2.0 1 04-09-2006 10:47 AM silv1983 ActionScript 2.0 1 03-27-2006 12:25 PM dpvtank ActionScript 1.0 (and below) 0 07-26-2002 05:53 PM jaredly ActionScript 1.0 (and below) 6 06-05-2002 07:56 PM

All times are GMT. The time now is 11:41 PM.

///