views:

46

answers:

2

Hi guys,

First of all, I am new to AS3. What I am trying to do is to make the ball, rotate in circular form. This is my current code: this is the Circular.as

public class Circular extends Sprite {

    private var ball:Ball;

    private var centerX:Number = stage.stageWidth / 2;
    private var centerY:Number = stage.stageHeight / 2;
    private var radiusX:Number = 500;
    private var radiusY:Number = 500;
    //private var temp:Number = 180;
    private static const speed:Number = 0.1; 

    public function Oval() {
        init();
    }

    private function init():void {
        ball = new Ball(40,0x00ff00,180.0);
        addChild(ball);
        ball.addEventListener(Event.ENTER_FRAME, startMoving(ball,ball.getAngle()));
    }

    private function startMoving(object:Ball, angle:Number) {
        return function(evt:Event){
                onStart(object,angle);
        }
    }

    private function onStart(object:Ball,angle:Number){
        object.x = centerX + Math.sin(angle) * radiusX;
        object.y = centerY + Math.cos(angle) * radiusY;
        ball.setAngle(speed);
        //trace(ball.getAngle());
    }

}

and this is the Ball.as class file

public class Ball extends Sprite {

    private var _radius:Number;
    private var _color:uint;
    private var _angle:Number;

    public function Ball(radius:Number,color:uint, angle:Number) {
        _radius = radius;
        _color  = color;
        _angle  = angle;
        init();
    }

    public function init():void {
        graphics.beginFill(_color);
        graphics.drawCircle(0,0,_radius);
        graphics.endFill();
    }

    public function getAngle():Number{
        return _angle;
    }

    public function setAngle(speed:Number){
        _angle += speed;
    }
}

I am sorry, I forgot to mention the problem I am facing now. The problem I have right now is, the ball did not move. Any ideas?

A: 

Your constructor should have the same name as your class. Here, your constructor is named Oval() when it should be named Circular(). The Circular class doesn't have a constructor so nothing will happen until you call the Oval() method

        //your class should be 
        public class Circular extends Sprite {

        //variables declarations

              //Your constructor
              public function Circular() {
               init();
              }
        //etc....

You could extend the Circular class with an Oval class if necessary.

        public class Oval extends Circular
        { 
             public function Oval()
             {
                super();
             }
        }

You may have to change some of your Circular variables from private to protected if you need to access them within the Oval class

PatrickS
Thanks. But i solved the problems myself. Its not the class name, I forgot to change it here :P What I did is change to this: object.x = centerX + Math.sin(object.getAngle()) * radiusX;Anyway, thanks for mentioning the class name
web_starter
+1  A: 

Ok, in your first class, you should not have a class of Circular and a constructor of Oval.

The problem was definitely with your ENTER_FRAME and how you were passing the angle/getting the new angle, etc. Here is an update that works for me. The animation could be smoothed, and there's a bunch you can do to help that, but this works. Make sure that your stage size is large enough depending on your radiusX/radiusY, had to lower it for my example to really see what was going on but now the ball moves in a circle.

Circular.as

import flash.display.Sprite;
import flash.events.Event;
import Ball;

public class Circular extends Sprite {

    private var ball:Ball;

    private var centerX:Number = stage.stageWidth / 2;
    private var centerY:Number = stage.stageHeight / 2;
    private var radiusX:Number = 200;
    private var radiusY:Number = 200;
    //private var temp:Number = 180;
    private static const speed:Number = 0.1; 

    public function Circular() {
        init();
    }

    private function init():void {
        ball = new Ball(40,0x00ff00,180.0);
        addChild(ball);
        ball.addEventListener(Event.ENTER_FRAME, moveBall);
    }

    private function moveBall(e:Event){
        ball.x = centerX + Math.sin(ball.getAngle()) * radiusX;
        ball.y = centerY + Math.cos(ball.getAngle()) * radiusY;
        ball.setAngle(speed);
        //trace(ball.getAngle());
    }

}

Ball.as unchanged.

Tegeril
That's right, you are calling ball.getAngle() just one time and the angle is always the same. You have to call getAngle() in every frame
unkiwii