Skip to main content
fanglinyong
Inspiring
April 20, 2010
Question

how can i run this code correctly?

  • April 20, 2010
  • 1 reply
  • 602 views

hello, everybody:

    when  i run the code, i found that it dons't work,so ,i  hope somebody can help me!

    the Ball class is this :
    package {
    import flash.display.Sprite;
    [SWF(width = "550", height = "400")]
    public class Ball extends Sprite {
        private var radius:Number;
        private var color:uint;
        private var vx:Number;
        private var vy:Number;
        public function Ball(radius:Number=40, color:uint=0xff9900,
        vx:Number =0, vy:Number =0) {
            this.radius= radius;
            this.color = color;
            this.vx = vx;
            this.vy = vy;
            init();
        }
        private function init():void {
            graphics.beginFill(color);
            graphics.drawCircle(0,0,radius);
            graphics.endFill();
        }
    }
}


and the Chain class code is :

  
package {
    import flash.display.Sprite;
    import flash.events.Event;
    [SWF(width = "550", height = "400")]
   
    public class Chain extends Sprite {
        private var ball0:Ball;
        private var ball1:Ball;
        private var ball2:Ball;
        private var spring:Number = 0.1;
        private var friction:Number = 0.8;
        private var gravity:Number = 5;
        //private var vx:Number =0;
        //private var vy:Number = 0;
       
        public function Chain() {
            init();
        }
       
        public function init():void {
            ball0  = new Ball(20);
            addChild(ball0);
            ball1 = new Ball(20);
            addChild(ball1);
            ball2 = new Ball(20);
            addChild(ball2);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
       
        private function onEnterFrame(event:Event):void {
            moveBall(ball0, mouseX, mouseY);
            moveBall(ball1, ball0.x, ball0.y);
            moveBall(ball2, ball1.x, ball1.y);
           
            graphics.clear();
            graphics.lineStyle(1);
            graphics.moveTo(mouseX, mouseY);
            graphics.lineTo(ball0.x, ball0.y);
            graphics.lineTo(ball1.x, ball1.y);
            graphics.lineTo(ball2.x, ball2.y);
           
        }
       
        private function moveBall(ball:Ball,
                                  targetX:Number,
                                  targetY:Number):void {
          
            ball.vx += (targetX - ball.x) * spring;
            ball.vy += (targetY - ball.y) * spring;
            ball.vy += gravity;
            ball.vx *= friction;
            ball.vy *= friction;
            ball.x += vx;
            ball.y += vy;
        }
    }
}

thanks every body's help!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 20, 2010

Can you explain what about your code is not working?  I notice your Chain class uses the Ball class quite a bit, but never imports it to be able to use it that I can see.  Maybe that is the problem.  If you are getting error messages, you should include them as well.

fanglinyong
Inspiring
April 20, 2010

when i run this code ,the console told me the Chail class can't visit the ball.pv property!

2010-04-20

鍊氬ぉ鐓ф捣鑺辨棤鏁帮紝娴佹按楂樺北蹇冭嚜濡傦紒

鍙戜欢浜? Ned Murphy <forums@adobe.com>

鍙戦€佹椂闂? 2010-04-20 21:57

涓? 棰? how  can i run this code correctly?

鏀朵欢浜? fang alvin <fanglinyong_81@163.com>

Can you explain what about your code is not working?聽 I notice your Chain class uses the Ball class quite a bit, but never imports it to be able to use it that I can see.聽 Maybe that is the problem.

Ned Murphy
Legend
April 20, 2010

I don't see that the Ball class has a pv property, or that you even try to access a pv property in the Chain class.  All of the properties in your Ball class are declared as private, so you probably cannot access them directly.  They would need to be public.  Also, I still don't see where you import Ball in the chain class such that it can use it.