Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:27:49+00:00 2026-05-31T08:27:49+00:00

Below is my current code and i am trying to get my ball to

  • 0

Below is my current code and i am trying to get my ball to bounce off with the proper angle of refraction based on the random angled walls and direction of the ball…My problem is I have only taken a basic physics class and know the equation “angle of incidence = angle of refraction” also, this is my first year in as3 so my coding is rather crude. The angle seems to be off…the problem is with the code “Bullet.hitTestObject(myblockadeHolder[t])” Thanks guys.

stage.addEventListener(Event.ENTER_FRAME,rotate);
var _trueRotation:Number;
var _dx:Number;
var _dy:Number;
function rotate (e:Event){
// calculate rotation based on mouse X & Y
    _dx = Turret.x - stage.mouseX;
    _dy = Turret.y - stage.mouseY;

    // which way to rotate
    var rotateTo:Number = getDegrees(getRadians(_dx, _dy)); 

    // keep rotation positive, between 0 and 360 degrees
    if (rotateTo > Turret.rotation + 180) rotateTo -= 360;
    if (rotateTo < Turret.rotation - 180) rotateTo += 360;

    // ease rotation
    _trueRotation = (rotateTo - Turret.rotation - 90) / 3;

    // update rotation
    Turret.rotation += _trueRotation;           
}
//Turret Rotation
//Create an array to hold multiple sprites
var mySpriteHolder:Array = [];
//Create a counter to keep track of the number of sprites
var lbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxLB:int = 1;
//Keypress Code
stage.addEventListener(MouseEvent.CLICK, dropBullet);
//Function for the mouse event to fire bullet
function dropBullet(evt:MouseEvent):void{

     var bcos:Number = Math.cos((Turret.rotation - 90) * Math.PI / 180);
     var bsin:Number = Math.sin((Turret.rotation - 90) * Math.PI / 180);

     //starting x and y
     var startx:int = Turret.x + (70 * bcos);
     var starty:int = Turret.y + (70 * bsin);
    //calculates where the bullet needs to go by aiming in front of the gun
    var endx:int = Turret.x + (100 * bcos);
    var endy:int = Turret.y + (100 * bsin);

    var Bullet:MovieClip = new bullet();

    Bullet.x = startx;
    Bullet.y = starty;

    Bullet.xspeed = (endx - startx)/5;
    Bullet.yspeed =  (endy - starty)/5;

    mySpriteHolder.push(Bullet);
    stage.addChild(Bullet);

    //this calls the move down function
    stage.addEventListener(Event.ENTER_FRAME,BulletFire);
}
//Function to shoot bullet
var Points:Number = 0;
var Life:Number = 100;
stage.addEventListener(Event.ENTER_FRAME, TextCounter);
function BulletFire(evt:Event):void{
    var Bullet:MovieClip;
    //Use a for loop to move the Bullets
    for(var i:int=mySpriteHolder.length-1; i>=0; i--){
        Bullet = mySpriteHolder[i];
    //Bounds Collision
    if(Bullet.hitTestObject(Up)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Lower)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Left)){
        Bullet.xspeed*=-1;
    }
    if(Bullet.hitTestObject(Right)){
        Bullet.xspeed*=-1;
    }
    if(Bullet.hitTestObject(Tank)){
        stage.removeChild(Bullet);
        mySpriteHolder.splice(i,1);
        lbCounter --;
        Life -= 10;
    }
    //Blockade Collision
    for(var t in myBlockadeHolder){
            if(Bullet.hitTestObject(myBlockadeHolder[t])){
                _trueRotation*=2
            var newAngle = (180 - (_trueRotation) - (smallangle))*-1;
            var newXspeed = Math.cos(newAngle);
            var newYspeed = Math.sin(newAngle);
            Bullet.xspeed = newXspeed+2.5;
            Bullet.yspeed = newYspeed+2.5;

            }
        }
    //Target Collision
        for(var c in mytargetHolder){
            if(Bullet.hitTestObject(mytargetHolder[c])){
            stage.removeChild(Bullet);
            mySpriteHolder.splice(i,1);
            lbCounter --;
            Points += 10;
            mytargetHolder[c].y = Math.random()*380 + 10;
            mytargetHolder[c].x = Math.random()*380 + 10;
                while(mytargetHolder[c].hitTestObject(Turret)){
                    mytargetHolder[c].y = Math.random()*380 + 10;
                    mytargetHolder[c].x = Math.random()*380 + 10;
                }
            }
            for(var a in mytargetHolder){
                for(var s in mytargetHolder){
                    while(mytargetHolder[a].hitTestObject(mytargetHolder[s])&& a!=s){
                        mytargetHolder[a].y = Math.random()*380 + 10;
                        mytargetHolder[a].x = Math.random()*380 + 10;
                    }
                }
                for(var g in myBlockadeHolder){
                    while(mytargetHolder[a].hitTestObject(myBlockadeHolder[g])&& a!=g){
                        mytargetHolder[a].y = Math.random()*380 + 10;
                        mytargetHolder[a].x = Math.random()*380 + 10;
                    }
                }
            }
        }
        Bullet.y += Bullet.yspeed;
        Bullet.x += Bullet.xspeed;
    }
}//Bullet Code
stage.addEventListener(Event.ENTER_FRAME, HealthCheck);
function HealthCheck(e:Event):void{
    if(Life<=0){
        stage.removeEventListener(MouseEvent.CLICK, dropBullet);
        stage.removeEventListener(Event.ENTER_FRAME, BulletFire);
        stage.removeEventListener(Event.ENTER_FRAME, droptarget);
        stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
    }
}//Health Code
//variables for blockade
var myblockadeSprite:Sprite;
//blockade is the linkage name in the library
var blockade:Blockade;
//Create an array to hold multiple sprites
var myBlockadeHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var LbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxlb:int = 6;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, dropblockade);
//Function for the mouse event to fire blockade
function dropblockade(evt:Event):void{
    for(var i:int=0;i<maxlb; i++){
        //PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
        //add the blockades to the canvas
        myblockadeSprite = new Sprite();
        stage.addChild(myblockadeSprite);
        //Get the actual picture from the library
        blockade = new Blockade();
        myblockadeSprite.addChild(blockade);
        //Going to load up the array with the sprites
        myBlockadeHolder[i] = myblockadeSprite;
        myBlockadeHolder[i].y = Math.random()*390 + 10;
        myBlockadeHolder[i].x = Math.random()*390 + 10;
        myBlockadeHolder[i].rotation = Math.random()*360;
        while(myBlockadeHolder[i].hitTestObject(Tank)){
            myBlockadeHolder[i].y = Math.random()*390 + 10;
            myBlockadeHolder[i].x = Math.random()*390 + 10;
        }   
    }
    for(var t:int=0;t<maxlb; t++){
        for(var d:int=0;d<maxlb; d++){
            while(myBlockadeHolder[t].hitTestObject(myBlockadeHolder[d])&& t!=d){
            myBlockadeHolder[t].y = Math.random()*390 + 10;
            myBlockadeHolder[t].x = Math.random()*390 + 10;
            }
        }
    }
    stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
}//Blockade Code
//variables for target
var mytargetSprite:Sprite;
//target is the linkage name in the library
var target:Target;
//Create an array to hold multiple sprites
var mytargetHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var TargetCounter:int = 0;
//Maximum number of sprites on the canvas
var maxtrgs:int = 3;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, droptarget);
function droptarget(evt:Event):void{
    for(var i:int=0;i<maxtrgs; i++){
        //PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
        //add the targets to the canvas
        mytargetSprite = new Sprite();
        stage.addChild(mytargetSprite);
        //Get the actual picture from the library
        target = new Target();
        mytargetSprite.addChild(target);
        //Going to load up the array with the sprites
        mytargetHolder[i] = mytargetSprite;
        mytargetHolder[i].y = Math.random()*390 + 10;
        mytargetHolder[i].x = Math.random()*390 + 10;
        while(mytargetHolder[i].hitTestObject(Tank)){
            mytargetHolder[i].y = Math.random()*390 + 10;
            mytargetHolder[i].x = Math.random()*390 + 10;
        }   
    }
    for(var t:int=0;t<maxtrgs; t++){
        for(var d:int=0;d<maxtrgs; d++){
            while(mytargetHolder[t].hitTestObject(mytargetHolder[d])&& t!=d){
            mytargetHolder[t].y = Math.random()*390 + 10;
            mytargetHolder[t].x = Math.random()*390 + 10;
            }
        }
        for(var w:int=0;w<maxtrgs; w++){
            while(mytargetHolder[t].hitTestObject(myBlockadeHolder[w])&& t!=w){
            mytargetHolder[t].y = Math.random()*390 + 10;
            mytargetHolder[t].x = Math.random()*390 + 10;
            }
        }
    }
    stage.removeEventListener(Event.ENTER_FRAME, droptarget);
}//Target Code
function getRadians(delta_x:Number, delta_y:Number):Number{
    var r:Number = Math.atan2(delta_y, delta_x);

    if (delta_y < 0){
        r += (2 * Math.PI);
        }
    return r;
}

/**
* Get degrees
* @param    radians Takes radians
* @return   Returns degrees
*/
function getDegrees(radians:Number):Number{
    return Math.floor(radians/(Math.PI/180));
}
function TextCounter(e:Event):void{
    PointCounter.text = String(Points);
    LifeCounter.text = String(Life);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:27:50+00:00Added an answer on May 31, 2026 at 8:27 am

    I’m not really a physics guy but I can refer you to an article/tutorial that will most likely help you solve this issue. If it does, please post your fixed code as an answer because that’s a better/more direct answer for this question. If not, hopefully someone else will come along with a better answer:

    http://blog.generalrelativity.org/actionscript-30/dynamic-circlecircle-collision-detection-in-actionscript-3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get an IFrame inner HTML using below code. <iframe src=http://www.msn.com width=100%
I'm trying to get the current system file cache size as shown below. However
In the code below, I am trying to get back the index of the
Hey all i am trying to get my code below to work in order
With the below javascript code i am trying some BackboneJs concepts. Couldn't figure out
I'm trying to use the code below to add an entry in the 'UserAlertSubscriptions'
Currently I’m trying to get the hang of a block copy with my current
Trying to get more than just the stock information at the current time period,
My code works in IE but not in Firefox. I am trying to get
I'm trying to get a day fraction for the current day to align a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.