2016-08-25 165 views
0

所以我正在阅读Keith Peters撰写的“ActionScript 3.0 Animation〜Making Things Move”一书,其中一个例子就是教授Parent Boxs ......我写了这些代码,执行后,它运行,但没有提供错误,没有任何反应,没有任何Sprite被绘制,它是一个空白的画布..?使用Flash Pro CS 6,12.0.2.529。我还没有任何其他例子的问题,并且.as“ParentBox”运行良好,当我尝试运行ParentBox2时是当我遇到此问题....思考? (对不起,很新的OOP,努力学习,就像我可以,而且这个网站特别有惊人的奔迄今为止对知识的巨大财富....培训代码不能正常工作

ParentBox.as

package { 
    import flash.display.Sprite; 
    public class ParentBox extends Sprite { 
     public function ParentBox() 
     { 
      init(); 
     } 
     private function init():void{ 
      graphics.lineStyle(1, 0); 
      graphics.drawRect(-50, -50, 100, 100); 

     } 
    }} 

ParentBox2.as代码....

package { 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 

    public class ParentBox2 extends Sprite { 
     private var parent1:ParentBox; 
     private var parent2:ParentBox; 
     private var ball:Sprite; 

    public function Reparenting2(){ 
     init(); 
    } 
    private function init():void{ 
     parent1 = new ParentBox(); 
     addChild(parent1); 
     parent1.x = 60; 
     parent1.y = 60; 

     parent2 = new ParentBox(); 
     addChild(parent2); 
     parent2.x = 170; 
     parent2.y = 60; 

     ball = new Sprite(); 
     parent1.addChild(ball); 
     ball.graphics.beginFill(0xff0000); 
     ball.graphics.drawCircle(0, 0, 40); 
     ball.graphics.endFill(); 
     ball.addEventListener(MouseEvent.CLICK, onBallClick); 
    } 
    public function onBallClick(event:MouseEvent):void{  
     parent2.addChild(ball); 
    } 
}} 

回答

0

public class ParentBox2 extends Sprite { 
需要

重命名为

public class Reparenting2 extends Sprite { 

功能...就像我说的,我还在学习,特别是命名的东西,谢谢大家!

1

您已经找到了答案,但对于任何一个其他人有同样的问题,

在ActionScript3的构造函数应具有相同的名称作为类名。

package { 
import flash.display.Sprite; 
import flash.events.MouseEvent; 

public class ParentBox2 extends Sprite { 
    private var parent1:ParentBox; 
    private var parent2:ParentBox; 
    private var ball:Sprite; 

public function ParentBox2(){ //the constructor function's name should be the same as that of the class. 
    init(); 
} 
...