TypeError: Error #1009 for Card Game [as3]

  • wjordan87
  • Born
  • Born
  • wjordan87
  • Posts: 2

Post 3+ Months Ago

Here is my code...

I think the problem has something to do with one of the vars being established when it shouldn't but I'm not sure.

ACTIONSCRIPT Code: [ Select ]
//--------IMPORT AWAY 3D-------//
import away3d.cameras.*;
import away3d.containers.*;
import away3d.materials.*;
import away3d.primitives.Plane;
import away3d.containers.ObjectContainer3D;
import away3d.core.math.Number3D;
 
//------IMPORT TWEENERS-------//
import caurina.transitions.*;
import flash.display.InteractiveObject;
import flash.display.Bitmap;
import flashx.textLayout.conversion.PlainTextExporter;
import flash.display.BitmapData;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
 
//-------SET UP VARIABLES------//
//variable for the scene
var scene:Scene3D;
//variable for camera - angle
var camera:Camera3D;
//variable for view
var view:View3D;
//number of cards that you want to have
var totalchildren:int = 10;
//set up array to hold the cards
var cards:Array;
//array to hold the front of the cards
var textures:Array = [new texture0(0,0),
                 new texture1(0,0),
                 new texture2(0,0),
                 new texture3(0,0),
                 new texture4(0,0)];
//get the back of the cards
var backtexture:BitmapData = new CardBack(0,0);
//set width and height
var cardwidth:Number = 150;
var cardheight:Number = 205;
//distance between each card
var xoffset:Number = 10;
var yoffset:Number = 10;
//create an object to hold all the cards
var cardsholder:ObjectContainer3D;
//create the sides of the selected card
var selectedCard1:Plane;
var selectedCard2:Plane;
//disable mouse events - gives us control over every card
var disableMouseEvents:Boolean = false;
 
 
 
 
 
 
//------------FUNCTIONS-------------//
 
//----------Establish Scene/Viewing Area---------//
function initAway3D():void{
   var scene = new Scene3D;
   var camera = new Camera3D;
   //position the camera
   camera.y = 1000; //works like a zoom
   camera.z = 0;
   camera.lookAt(new Number3D(0,0,0));
   //create and position the view
   var view = new View3D({scene:scene, camera:camera});
   //put the view on the stage
   view.x = stage.stageWidth/2;
   view.y = stage.stageHeight/2;
   addChild(view);
}
 
//-----Create One of the Carfds------//
function createCard(texture:BitmapData, id:int):ObjectContainer3D{
   var card:ObjectContainer3D = new ObjectContainer3D();
   //var for the front
   var front:Plane = new Plane({width:cardwidth, height:cardheight, material:new BitmapMaterial(texture,{smmoth:true})});
   //var for the back
   var back:Plane = new Plane({width:cardwidth, height:cardheight, material:new BitmapMaterial(backtexture,{smmoth:true})});
   //positioning
   front.rotationY = 180;
   back.rotationZ = 180;
   back.rotationY = 180;
   
   //access id - use to determine the matches
   back.extra = {};
   back.extra.id = id;
   back.extra. targetCard = card;
   back.addOnMouseDown(onBackClicked);
   
   //spin moves
   card.rotationZ = 180;
   card.addChild(front);
   card.addChild(back);
   card.ownCanvas = true;
   
   //create the card
   return card;
 
}
 
 
//--------Create the rest of the cards----------//
function allCards():void{
   var cards = new Array();
   //(starts at 0, goes until it reaches the end of array, increases by 1)
   for(var i:int=0; i<textures.length; i++){
      //create the first card
      var card1:ObjectContainer3D = createCard(textures[i],i);
      //create it's match
      var card2:ObjectContainer3D = createCard(textures[i],i);
      //create it's match
      //var card3:ObjectContainer3D = create Card(textures[i],i);
      //create it's match
      //var card4:ObjectContainer3D = create Card(textures[i],i);
      cards.push(card1);
      cards.push(card2);
   }
}
 
//-------- SHUFFLE/RANDOMIZE CARDS---------//
function randomizeCards():void{
   //var to hold random order of cards
   var newArray:Array = new Array();
   while(cards.length>0){
      newArray.push(cards.splice(Math.floor(Math.random()*cards.length),1)[0]);
   }
   cards = newArray;
}
 
//-------Add Cards to the Scene-------//
function addCardsToScene():void{
   //var that holds everything
   var cardsholder = new ObjectContainer3D();
   //how many cards we currently have
   var cardNumber:int = 0;
   //create the grid
   //vertical part of the grid - number of rows
   for(var i:int = 0; i<2; i++){
      //number of columns
      for(var b:int = 0; b<5; b++){
         cards[cardNumber].x = b*(cardwidth+xoffset)+cardwidth/2;
         cards[cardNumber].z = i*(cardheight+yoffset)+cardheight/2;
         cardsholder.addChild(cards[cardNumber]);
         cardNumber++;
      }
   }
   var cardswidth:Number = (5*cardwidth) + (4*xoffset);
   var cardsheight:Number = (2*cardheight) + (1*yoffset);
   
   //position
   cardsholder.x = cardswidth/2;
   cardsholder.y = cardsheight/2;
   
   //add to the scene
   scene.addChild(cardsholder);
}
 
//-------------------CLICK FUNCTIONS-----------------//
function onBackClicked(event:Event):void{
   if(disableMouseEvents==false){
      if(selectedCard1==null){
         selectedCard1 = event.currentTarget as Plane;
      }
      else{
         if(selectedCard2==null){
            selectedCard2=event.currentTarget as Plane;
            waitForDecision();
            disableMouseEvents=true;
         }
      }
      Tweener.addTween(event.currentTarget.extra.targetCard,{y:50, rotationZ:0, time:1});
   }
}
 
//---------DEFINE THE DECISION-----------//
function waitForDecision():void{
   var timer:Timer = new Timer(1000, 1);
   timer.addEventListener(TimerEvent.TIMER, makeDecision);
   timer.start();
}
 
//----------WHAT TO DO IF RIGHT OR WRONG-----------//
function makeDecision(event:Event):void{
   //if it matches = right
   if(selectedCard1.extra.id == selectedCard2.extra.id){
      Tweener.addTween(selectedCard1.extra.targetCard,{alpha:0, time:0.3, onComplete:removeCard, onCompleteParams:[selectedCard1.extra.targetCard]});
      Tweener.addTween(selectedCard2.extra.targetCard,{alpha:0, time:0.3, onComplete:removeCard, onCompleteParams:[selectedCard1.extra.targetCard]});
 
   }
   //if its wrong
   else{
      Tweener.addTween(selectedCard1.extra.targetCard,{y:0, rotationZ:180, time:1});
      Tweener.addTween(selectedCard2.extra.targetCard,{y:0, rotationZ:180, time:1});
   }
   //reset variables for mouse clicks, and card slection
   disableMouseEvents = false;
   selectedCard1=null;
   selectedCard2=null;
}
 
//-------DEFINE REMOVE CARD FUNCTION----------//
function removeCard(event:ObjectContainer3D):void{
   cardsholder.removeChild(event);
   totalchildren--;
   if(totalchildren==0){
      trace("WIN");
   }
}
 
//---------------RENDER EVERYTHING-------------//
function startToRender():void{
   addEventListener(Event.ENTER_FRAME, render);
}
 
function render(event:Event):void{
   view.render();
}
 
//------------CALL ALL THE FUNCTIONS---------//
initAway3D();
allCards();
randomizeCards();
addCardsToScene();
startToRender();
  1. //--------IMPORT AWAY 3D-------//
  2. import away3d.cameras.*;
  3. import away3d.containers.*;
  4. import away3d.materials.*;
  5. import away3d.primitives.Plane;
  6. import away3d.containers.ObjectContainer3D;
  7. import away3d.core.math.Number3D;
  8.  
  9. //------IMPORT TWEENERS-------//
  10. import caurina.transitions.*;
  11. import flash.display.InteractiveObject;
  12. import flash.display.Bitmap;
  13. import flashx.textLayout.conversion.PlainTextExporter;
  14. import flash.display.BitmapData;
  15. import flash.events.Event;
  16. import flash.utils.Timer;
  17. import flash.events.TimerEvent;
  18.  
  19. //-------SET UP VARIABLES------//
  20. //variable for the scene
  21. var scene:Scene3D;
  22. //variable for camera - angle
  23. var camera:Camera3D;
  24. //variable for view
  25. var view:View3D;
  26. //number of cards that you want to have
  27. var totalchildren:int = 10;
  28. //set up array to hold the cards
  29. var cards:Array;
  30. //array to hold the front of the cards
  31. var textures:Array = [new texture0(0,0),
  32.                  new texture1(0,0),
  33.                  new texture2(0,0),
  34.                  new texture3(0,0),
  35.                  new texture4(0,0)];
  36. //get the back of the cards
  37. var backtexture:BitmapData = new CardBack(0,0);
  38. //set width and height
  39. var cardwidth:Number = 150;
  40. var cardheight:Number = 205;
  41. //distance between each card
  42. var xoffset:Number = 10;
  43. var yoffset:Number = 10;
  44. //create an object to hold all the cards
  45. var cardsholder:ObjectContainer3D;
  46. //create the sides of the selected card
  47. var selectedCard1:Plane;
  48. var selectedCard2:Plane;
  49. //disable mouse events - gives us control over every card
  50. var disableMouseEvents:Boolean = false;
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. //------------FUNCTIONS-------------//
  58.  
  59. //----------Establish Scene/Viewing Area---------//
  60. function initAway3D():void{
  61.    var scene = new Scene3D;
  62.    var camera = new Camera3D;
  63.    //position the camera
  64.    camera.y = 1000; //works like a zoom
  65.    camera.z = 0;
  66.    camera.lookAt(new Number3D(0,0,0));
  67.    //create and position the view
  68.    var view = new View3D({scene:scene, camera:camera});
  69.    //put the view on the stage
  70.    view.x = stage.stageWidth/2;
  71.    view.y = stage.stageHeight/2;
  72.    addChild(view);
  73. }
  74.  
  75. //-----Create One of the Carfds------//
  76. function createCard(texture:BitmapData, id:int):ObjectContainer3D{
  77.    var card:ObjectContainer3D = new ObjectContainer3D();
  78.    //var for the front
  79.    var front:Plane = new Plane({width:cardwidth, height:cardheight, material:new BitmapMaterial(texture,{smmoth:true})});
  80.    //var for the back
  81.    var back:Plane = new Plane({width:cardwidth, height:cardheight, material:new BitmapMaterial(backtexture,{smmoth:true})});
  82.    //positioning
  83.    front.rotationY = 180;
  84.    back.rotationZ = 180;
  85.    back.rotationY = 180;
  86.    
  87.    //access id - use to determine the matches
  88.    back.extra = {};
  89.    back.extra.id = id;
  90.    back.extra. targetCard = card;
  91.    back.addOnMouseDown(onBackClicked);
  92.    
  93.    //spin moves
  94.    card.rotationZ = 180;
  95.    card.addChild(front);
  96.    card.addChild(back);
  97.    card.ownCanvas = true;
  98.    
  99.    //create the card
  100.    return card;
  101.  
  102. }
  103.  
  104.  
  105. //--------Create the rest of the cards----------//
  106. function allCards():void{
  107.    var cards = new Array();
  108.    //(starts at 0, goes until it reaches the end of array, increases by 1)
  109.    for(var i:int=0; i<textures.length; i++){
  110.       //create the first card
  111.       var card1:ObjectContainer3D = createCard(textures[i],i);
  112.       //create it's match
  113.       var card2:ObjectContainer3D = createCard(textures[i],i);
  114.       //create it's match
  115.       //var card3:ObjectContainer3D = create Card(textures[i],i);
  116.       //create it's match
  117.       //var card4:ObjectContainer3D = create Card(textures[i],i);
  118.       cards.push(card1);
  119.       cards.push(card2);
  120.    }
  121. }
  122.  
  123. //-------- SHUFFLE/RANDOMIZE CARDS---------//
  124. function randomizeCards():void{
  125.    //var to hold random order of cards
  126.    var newArray:Array = new Array();
  127.    while(cards.length>0){
  128.       newArray.push(cards.splice(Math.floor(Math.random()*cards.length),1)[0]);
  129.    }
  130.    cards = newArray;
  131. }
  132.  
  133. //-------Add Cards to the Scene-------//
  134. function addCardsToScene():void{
  135.    //var that holds everything
  136.    var cardsholder = new ObjectContainer3D();
  137.    //how many cards we currently have
  138.    var cardNumber:int = 0;
  139.    //create the grid
  140.    //vertical part of the grid - number of rows
  141.    for(var i:int = 0; i<2; i++){
  142.       //number of columns
  143.       for(var b:int = 0; b<5; b++){
  144.          cards[cardNumber].x = b*(cardwidth+xoffset)+cardwidth/2;
  145.          cards[cardNumber].z = i*(cardheight+yoffset)+cardheight/2;
  146.          cardsholder.addChild(cards[cardNumber]);
  147.          cardNumber++;
  148.       }
  149.    }
  150.    var cardswidth:Number = (5*cardwidth) + (4*xoffset);
  151.    var cardsheight:Number = (2*cardheight) + (1*yoffset);
  152.    
  153.    //position
  154.    cardsholder.x = cardswidth/2;
  155.    cardsholder.y = cardsheight/2;
  156.    
  157.    //add to the scene
  158.    scene.addChild(cardsholder);
  159. }
  160.  
  161. //-------------------CLICK FUNCTIONS-----------------//
  162. function onBackClicked(event:Event):void{
  163.    if(disableMouseEvents==false){
  164.       if(selectedCard1==null){
  165.          selectedCard1 = event.currentTarget as Plane;
  166.       }
  167.       else{
  168.          if(selectedCard2==null){
  169.             selectedCard2=event.currentTarget as Plane;
  170.             waitForDecision();
  171.             disableMouseEvents=true;
  172.          }
  173.       }
  174.       Tweener.addTween(event.currentTarget.extra.targetCard,{y:50, rotationZ:0, time:1});
  175.    }
  176. }
  177.  
  178. //---------DEFINE THE DECISION-----------//
  179. function waitForDecision():void{
  180.    var timer:Timer = new Timer(1000, 1);
  181.    timer.addEventListener(TimerEvent.TIMER, makeDecision);
  182.    timer.start();
  183. }
  184.  
  185. //----------WHAT TO DO IF RIGHT OR WRONG-----------//
  186. function makeDecision(event:Event):void{
  187.    //if it matches = right
  188.    if(selectedCard1.extra.id == selectedCard2.extra.id){
  189.       Tweener.addTween(selectedCard1.extra.targetCard,{alpha:0, time:0.3, onComplete:removeCard, onCompleteParams:[selectedCard1.extra.targetCard]});
  190.       Tweener.addTween(selectedCard2.extra.targetCard,{alpha:0, time:0.3, onComplete:removeCard, onCompleteParams:[selectedCard1.extra.targetCard]});
  191.  
  192.    }
  193.    //if its wrong
  194.    else{
  195.       Tweener.addTween(selectedCard1.extra.targetCard,{y:0, rotationZ:180, time:1});
  196.       Tweener.addTween(selectedCard2.extra.targetCard,{y:0, rotationZ:180, time:1});
  197.    }
  198.    //reset variables for mouse clicks, and card slection
  199.    disableMouseEvents = false;
  200.    selectedCard1=null;
  201.    selectedCard2=null;
  202. }
  203.  
  204. //-------DEFINE REMOVE CARD FUNCTION----------//
  205. function removeCard(event:ObjectContainer3D):void{
  206.    cardsholder.removeChild(event);
  207.    totalchildren--;
  208.    if(totalchildren==0){
  209.       trace("WIN");
  210.    }
  211. }
  212.  
  213. //---------------RENDER EVERYTHING-------------//
  214. function startToRender():void{
  215.    addEventListener(Event.ENTER_FRAME, render);
  216. }
  217.  
  218. function render(event:Event):void{
  219.    view.render();
  220. }
  221.  
  222. //------------CALL ALL THE FUNCTIONS---------//
  223. initAway3D();
  224. allCards();
  225. randomizeCards();
  226. addCardsToScene();
  227. startToRender();
Moderator Remark: Inserted code in the topic with [actionscript] bbcode. Better chance of replies this way.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

The error you are receiving, does it happen to say what line number that is for?
  • wjordan87
  • Born
  • Born
  • wjordan87
  • Posts: 2

Post 3+ Months Ago

This is the error in full


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at jordanCardGame_fla::MainTimeline/randomizeCards()
at jordanCardGame_fla::MainTimeline/frame1()
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9101
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Not familiar with this error, but I looked it up and it seems that others who had this problem solved it by putting a blank frame at the beginning of the loaded swf. Try that and see if that does anything. A few others mentioned that the event listeners were causing problems for some reason.

Is this an externally called SWF, or can you explain how this SWF is supposed to work. If I am unable to help you it might provide enough information for someone else with more experience with ActionScript to help you.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 7 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.