A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [Help]/[Problem] Help with Archery Sim

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    2

    [Help]/[Problem] Help with Archery Sim

    Hi, I have been looking around all over the web to find useful hints and code that can help me accomplish what I am trying to do.

    I want to create a archery game. Yes I know, its been done too many times, right? But I got interested in doing one as my first game. Now I just started reading up on ActionScript 2.0 and I am using Flash 8 Pro for this game. I am at the point where I want to simulate the flight of an arrow, like it would fly in the real world. So right now, I want to shoot a arrow (at a constant speed and angle, for now) and I want it to fly across the screen as it would in real life.

    The archery game environment is from left to right, meaning you start off from the left and enemies com from the right. And you shoot to the right (but can also shoot to the left, up down, and any other direction) you can also move around freely to a certain point.

    I really suck at this trig stuff you have to use to simulate the flight of a arrow but right now, this is what I have for code. NOTE: this code is placed onto the first frame on a layer called "actions" the actual arrow (which is a movie clip) is on another layer called "arrow" with a instance name of arrow1 and movie clip name of arrow. Also the registration point on the arrow MC is in the middle, and I drew the arrow facing to the right so like this: ---->

    Code:
    var angle = 45;
    var rAngle = angle - 90;
    
    arrow1.onEnterFrame = function(){
    	var speed = 5;
    	
    	this._x += speed * Math.sin( angle * Math.PI / 180 );
    	this._y += speed * Math.cos( angle * Math.PI / 180 );
    	
    	this._rotation = rAngle;
    	angle -= 1;
    	rAngle += 1;
    };
    
    function rotation(dx:Number, dy:Number):Number {
       return Math.atan2(dy, dx) * 180/Math.PI;
    }
    So any help that anyone can give me will be greatly appreaciated. I have tried other forums but they never respond or have no information on what I am trying to do exactly.
    Last edited by gouf08; 08-07-2007 at 05:39 PM.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You have cos and sin mixed up and I am not sure why you need so several angle variables.

    PHP Code:
    var angle = -45;
    arrow1.onEnterFrame = function () {
        var 
    speed 5;
        
    angle += 1;
        
    this._x += speed Math.cos (angle Math.PI 180);
        
    this._y += speed Math.sin (angle Math.PI 180);
        
    this._rotation angle;
    }; 

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    2
    Woah? Your code works way smoother than my own. For that example I was trying to do the same thing you just did but look at the code quality difference. Thanks.

    Now I have to figure out how to use this to simulate the flight of an arrow? Anyone got suggestions? Any will be greatly appreciated.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Set a base speed for the x and y directions. Then, adjust them based on the angle of the direction you're firing.

    angle = directionYouWantToFireIn;


    Oh and if you make a global variable called deg2rad, make it equal to Math.PI / 180; and replace it in your current code, you'll eek out a little bit smoother gameplay.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

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