开发者

Why I cant set the position in this code (AS3)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 4 years ago.

Improve this question

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜