Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 4th, 2012  |  syntax: None  |  size: 12.59 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Making Subclass Objects Non-Null
  2. package
  3. {
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     import Document;
  7.     import RestartButton;
  8.  
  9.     public class McMain extends MovieClip
  10.     {
  11.         public var document:Document;
  12.         public var leftKeyDown:Boolean = false;
  13.         public var rightKeyDown:Boolean = false;
  14.         public var upKeyDown:Boolean = false;
  15.         public var downKeyDown:Boolean = false;
  16.         public var onGround:Boolean = true;
  17.         public var xSpeed:Number = 0;
  18.         public var ySpeed:Number = 0;
  19.         public var mainSpeed:Number = 3.75;
  20.         public var frictionPower:Number = 0.9;
  21.         public var jumpPower:Number = 13;
  22.         public var gravityPower:Number = 0.5;
  23.         public var terminalVelocity:Number = 75;
  24.  
  25.         public function McMain()
  26.         {
  27.             this.addEventListener(Event.ADDED_TO_STAGE, initMcMain);
  28.             // constructor code
  29.         }
  30.         public function initMcMain(event:Event)
  31.         {
  32.             addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
  33.             addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
  34.             addEventListener(Event.ENTER_FRAME, hitTest);
  35.             addEventListener(Event.ENTER_FRAME, Main);
  36.         }
  37.         public function Main(event:Event):void
  38.         {
  39.             this.moveCharacter();
  40.             this.dynamicMovement();
  41.         }
  42.         public function checkKeysDown(event:KeyboardEvent):void
  43.         {
  44.             if (event.keyCode == 37)
  45.             {
  46.                 this.leftKeyDown = true;
  47.             }
  48.             if (event.keyCode == 38)
  49.             {
  50.                 this.upKeyDown = true;
  51.             }
  52.             if (event.keyCode == 39)
  53.             {
  54.                 this.rightKeyDown = true;
  55.             }
  56.             if (event.keyCode == 40)
  57.             {
  58.                 this.downKeyDown = true;
  59.             }
  60.         }
  61.         public function checkKeysUp(event:KeyboardEvent):void
  62.         {
  63.             if (event.keyCode == 37)
  64.             {
  65.                 this.leftKeyDown = false;
  66.             }
  67.             if (event.keyCode == 38)
  68.             {
  69.                 this.upKeyDown = false;
  70.             }
  71.             if (event.keyCode == 39)
  72.             {
  73.                 this.rightKeyDown = false;
  74.             }
  75.             if (event.keyCode == 40)
  76.             {
  77.                 this.downKeyDown = false;
  78.             }
  79.         }
  80.         public function moveCharacter():void
  81.         {
  82.             if (this.leftKeyDown)
  83.             {
  84.                 this.xSpeed -=  this.mainSpeed;
  85.                 if (this.onGround)
  86.                 {
  87.                     this.scaleX = -1;
  88.                 }
  89.             }
  90.             if (this.rightKeyDown)
  91.             {
  92.                 this.xSpeed +=  this.mainSpeed;
  93.                 if (this.onGround)
  94.                 {
  95.                     this.scaleX = 1;
  96.                 }
  97.             }
  98.             if (this.leftKeyDown && this.onGround || this.rightKeyDown && this.onGround)
  99.             {
  100.                 this.gotoAndStop(2);
  101.             }
  102.             if (this.currentFrame == 2)
  103.             {
  104.                 if (! this.leftKeyDown && ! this.rightKeyDown)
  105.                 {
  106.                     this.gotoAndStop(3);
  107.                 }
  108.             }
  109.             if (this.currentFrame == 3)
  110.             {
  111.                 if (this.skidAnimation.currentFrame == this.skidAnimation.totalFrames)
  112.                 {
  113.                     this.gotoAndStop(1);
  114.                 }
  115.             }
  116.             if (this.upKeyDown)
  117.             {
  118.                 this.ySpeed -=  this.jumpPower;
  119.             }
  120.             if (this.upKeyDown && this.leftKeyDown)
  121.             {
  122.                 this.ySpeed -=  0;
  123.                 this.xSpeed -=  10;
  124.             }
  125.             if (this.upKeyDown && this.rightKeyDown)
  126.             {
  127.                 this.ySpeed -=  0;
  128.                 this.xSpeed +=  10;
  129.             }
  130.             if (this.xSpeed > 3 && ! onGround || this.xSpeed < -3 && ! onGround)
  131.             {
  132.                 if (this.currentFrame == 2)
  133.                 {
  134.                     this.gotoAndStop(5);
  135.                 }
  136.             }
  137.             if (this.ySpeed < -0.5 && ! onGround)
  138.             {
  139.                 this.gotoAndStop(4);
  140.             }
  141.             else if (this.ySpeed > 0.5 && ! onGround)
  142.             {
  143.                 this.gotoAndStop(5);
  144.             }
  145.             if (this.currentFrame == 5 && onGround)
  146.             {
  147.                 this.gotoAndStop(1);
  148.             }
  149.             //if (! leftKeyDown && ! rightKeyDown && ! upKeyDown)
  150.             //{
  151.             //mcMain.gotoAndStop(1);
  152.             //}
  153.         }
  154.         public function dynamicMovement():void
  155.         {
  156.             if (onGround)
  157.             {
  158.                 this.x +=  this.xSpeed;
  159.                 this.xSpeed *=  this.frictionPower;
  160.             }
  161.             else
  162.             {
  163.                 this.x +=  this.xSpeed;
  164.                 this.xSpeed *=  (this.frictionPower + 0.09);
  165.             }
  166.             if (this.xSpeed > 7)
  167.             {
  168.                 this.xSpeed = 7;
  169.             }
  170.             if (this.xSpeed < -7)
  171.             {
  172.                 this.xSpeed = -7;
  173.             }
  174.             this.y +=  this.ySpeed;
  175.             this.ySpeed +=  this.gravityPower;
  176.             if (this.ySpeed > this.terminalVelocity)
  177.             {
  178.                 this.ySpeed = this.terminalVelocity;
  179.             }
  180.         }
  181.         public function hitTest(event:Event)
  182.         {
  183.             //spawnArea.visible = false;
  184.             this.mcMainHitArea.visible = false;
  185.             this.document.levelChange.wallCollision.visible = false;
  186.             //^^^^^^^^^^^^^^^^^^^^^^^^^^
  187.             //Error 1009 appearing here!
  188.             this.document.levelChange.deathArea.visible = false;
  189.             this.document.levelChange.goalArea.goalHitArea.visible = false;
  190.  
  191.             while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
  192.             {
  193.                 this.y = this.y - 0.5;
  194.                 this.ySpeed = 0;
  195.             }
  196.             while (document.levelChange.wallCollision.hitTestPoint(this.x, this.y - 24, true))
  197.             {
  198.                 this.y = this.y + 0.5;
  199.                 this.ySpeed = 0;
  200.             }
  201.             while (document.levelChange.wallCollision.hitTestPoint(this.x - 12, this.y - 11, true))
  202.             {
  203.                 this.x = this.x + 0.5;
  204.                 this.xSpeed = 0;
  205.                 if (! onGround)
  206.                 {
  207.                     this.leftKeyDown = false;
  208.                 }
  209.             }
  210.             while (document.levelChange.wallCollision.hitTestPoint(this.x + 12, this.y - 11, true))
  211.             {
  212.                 this.x = this.x - 0.5;
  213.                 this.xSpeed = 0;
  214.                 if (! onGround)
  215.                 {
  216.                     this.rightKeyDown = false;
  217.                 }
  218.             }
  219.             if (document.levelChange.wallCollision.hitTestPoint(this.x - 16, this.y - 11, true))
  220.             {
  221.                 if (this.upKeyDown)
  222.                     {
  223.                         this.ySpeed -=  10;
  224.                         this.xSpeed +=  50;
  225.                     }
  226.             }
  227.             if (document.levelChange.wallCollision.hitTestPoint(this.x + 16, this.y - 11, true))
  228.             {
  229.                 if (this.upKeyDown)
  230.                     {
  231.                         this.ySpeed -=  10;
  232.                         this.xSpeed -=  50;
  233.                     }
  234.             }
  235.             if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y, true))
  236.             {
  237.                 if (this.upKeyDown)
  238.                 {
  239.                     this.upKeyDown = false;
  240.                     this.onGround = false;
  241.                 }
  242.             }
  243.             if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
  244.             {
  245.                 if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1.5, true))
  246.                 {
  247.                     this.upKeyDown = false;
  248.                 }
  249.                 if (! document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
  250.                 {
  251.                     //upKeyDown = false;
  252.                     onGround = false;
  253.                 }
  254.             }
  255.             if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 1, true))
  256.             {
  257.                 this.ySpeed = 0;
  258.                 if (document.levelChange.wallCollision.hitTestPoint(this.x, this.y + 5, true))
  259.                 {
  260.                     onGround = true;
  261.                 }
  262.             }
  263.             if (document.levelChange.deathArea.hitTestPoint(this.x, this.y + 1, true))
  264.             {
  265.                 this.x = this.x;
  266.                 this.y = this.y;
  267.             }
  268.             if (this.hitTestObject(document.levelChange.goalArea.goalHitArea))
  269.             {
  270.                 if (stage.contains(document.level_1))
  271.                 {
  272.                     this.removeChild(document.level_1);
  273.                     //stage.removeEventListener(Event.ENTER_FRAME,hitTest);
  274.                 }
  275.                 if (stage.contains(document.spawnArea))
  276.                 {
  277.                     this.x = this.x;
  278.                     this.y = this.y;
  279.                 }
  280.                 addChild(document.level_2);
  281.                 document.level_2.x = -1425;
  282.                 document.level_2.y = -2550;
  283.             }
  284.         }
  285.  
  286.     }
  287.  
  288. }
  289.        
  290. package
  291. {
  292.     import flash.events.*;
  293.     import flash.display.*;
  294.     import flash.geom.Point;
  295.     import McMain;
  296.     import RestartButton;
  297.     import Level_2;
  298.  
  299.     public class Document extends MovieClip
  300.     {
  301.         public var levelNumber:int = 1;
  302.         public var levelChange:Object;
  303.         public var levelArray:Array = new Array();
  304.         public var collisionArray:Array = new Array();
  305.         public var deathAreaArray:Array = new Array();
  306.         public var goalAreaArray:Array = new Array();
  307.         public var goalHitAreaArray:Array = new Array();
  308.         public var mcMain:McMain;
  309.         public var restartButton:RestartButton;
  310.         public var level_2:Level_2;
  311.  
  312.         public function Document()
  313.         {
  314.             addEventListener(Event.ADDED_TO_STAGE, init);
  315.             mcMain = new McMain();
  316.             mcMain.document = this;
  317.             restartButton = new RestartButton();
  318.             restartButton.document = this;
  319.             level_2 = new Level_2();
  320.             // constructor code
  321.         }
  322.         public function init(event:Event)
  323.         {
  324.             this.levelChange = this.level_1;
  325.         }
  326.         public function levelHandler(event:Event)
  327.         {
  328.             this.levelChange = this["level_" + levelNumber];
  329.             if (level_2.stage)
  330.             {
  331.                 levelNumber = 2;
  332.                 //trace(levelChange);
  333.             }
  334.             for (var i:int = numChildren - 1; i >= 0; i--)
  335.             {
  336.                 var collisionChild:DisplayObject = getChildAt(i);
  337.                 if (collisionChild.name == "wallCollision")
  338.                 {
  339.                     collisionArray.push(collisionChild);
  340.                 }
  341.                 var deathAreaChild:DisplayObject = getChildAt(i);
  342.                 if (deathAreaChild.name == "deathArea")
  343.                 {
  344.                     deathAreaArray.push(deathAreaChild);
  345.                 }
  346.                 var goalAreaChild:DisplayObject = getChildAt(i);
  347.                 if (goalAreaChild.name == "goalArea")
  348.                 {
  349.                     goalAreaArray.push(goalAreaChild);
  350.                 }
  351.             }
  352.             for (var i_2:int = numChildren - 2; i >= 0; i--)
  353.             {
  354.                 var goalHitAreaChild:DisplayObject = getChildAt(i_2);
  355.                 if (goalHitAreaChild.name == "goalHitArea")
  356.                 {
  357.                     goalHitAreaArray.push(goalHitAreaChild);
  358.                 }
  359.             }
  360.         }
  361.         public function vCamMovement(event:Event):void
  362.         {
  363.             /*for (var i:int = 0; i < this.numChildren - 1; i++)
  364.             {
  365.             this.getChildAt(i).x -=  xSpeed;
  366.             //levelObjects.getChildAt(i).y -=  ySpeed;
  367.             }*/
  368.             level_1.x +=  stage.stageWidth * 0.5 - mcMain.x;
  369.             level_1.y +=  stage.stageHeight * 0.5 - mcMain.y;
  370.             level_2.x +=  stage.stageWidth * 0.5 - mcMain.x;
  371.             level_2.y +=  stage.stageHeight * 0.5 - mcMain.y;
  372.             spawnArea.x +=  stage.stageWidth * 0.5 - mcMain.x;
  373.             spawnArea.y +=  stage.stageHeight * 0.5 - mcMain.y;
  374.             mcMain.x = stage.stageWidth * 0.5;
  375.             mcMain.y = stage.stageHeight * 0.5;
  376.         }
  377.  
  378.     }
  379.  
  380. }
  381.        
  382. TypeError: Error #1009: Cannot access a property or method of a null object reference. at McMain/hitTest()[C:Users*Desktop*Flash*McMain.as:186] *= XXXXXXXXXXXX
  383.        
  384. mcMain = new McMain();
  385. mcMain.document = this;
  386. restartButton = new RestartButton();
  387. restartButton.document = this;
  388.        
  389. if(document != null && document.levelChange != null)