A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: I'm getting ReferenceError: Error #1056: Cannot create property dx on bullet

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    7

    I'm getting ReferenceError: Error #1056: Cannot create property dx on bullet

    So, I wanted to create a simple game with a spaceship that shoots other spaceships, but so far I'm having one problem after another (I'm kinda new to ActionScript).

    First, I wanted the ship to rotate and face the cursor. I managed to do it with some help from Google, but then I experienced yet another problem with shooting bullets.
    I'll skip all my tries and failures, because it doesn't really matter, for I decided to rewrite the code from scratch.

    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.*;
    	
    	public class hraSamotna extends MovieClip
    	{
    		public var naboj:bullet;
    		public var vesmirnaLod:ship;
    		public var nepriatel:enemy;
    		
    		private const r2d = 180/Math.PI //radians to degrees
    		private var Shipdx:Number;
    		private var Shipdy:Number;
    		public var dx:Number;
    		public var dy:Number;
    		
    		public var nepriatelia:Array = []; //array of enemies (no enemies yet)
    		public var naboje:Array = [];	//array of bullets
    		public var rychlostStrely:int = 5; //shot speed
    		public var i:uint;
    		
    		private function createShip():void
    		{
    			vesmirnaLod = new ship();
    				vesmirnaLod.x = 300;
    				vesmirnaLod.y = 150;
    				
    			stage.addEventListener(MouseEvent.MOUSE_MOVE, otacanieLode);
    			addChild(vesmirnaLod);
    		}
    		private function otacanieLode(Event:MouseEvent):void //turns the ship towards the mouse
    		{
    			Shipdx = mouseX - vesmirnaLod.x;
    			Shipdy = mouseY - vesmirnaLod.y;
    			vesmirnaLod.rotation = Math.atan2(Shipdy, Shipdx) * r2d;
    		}
    		public function hraSamotna():void
    		{
    			createShip();
    			
    			stage.addEventListener(MouseEvent.CLICK, strelba);
    		}
    		public function strelba(e:MouseEvent):void
    		{
    			naboj = new bullet();
    				naboj.x = vesmirnaLod.x;
    				naboj.y = vesmirnaLod.y;
    				naboje.push(naboj);
    				
    			naboj.addEventListener(Event.ENTER_FRAME, pohybNaboja);
    			addChild(naboj);
    		}
    		public function pohybNaboja(e:Event)
    		{
    			for(i = 0; i<naboje.length - 1; i++)
    			{
    				naboje[i].dx = mouseX - naboje[i].x;
    				naboje[i].dy = mouseY - naboje[i].y;
    				pohybNaboja2(naboje[i].dy, naboje[i].dx);
    			}
    		}
    		public function pohybNaboja2(cislo1:Number, cislo2:Number)
    		{
    			var rads:Number = Math.atan2(cislo1, cislo2);
    			
    			naboje[i].x += Math.cos( rads) * rychlostStrely;
    			naboje[i].y += Math.sin( rads) * rychlostStrely;
    		}
    	}
    }
    I'm now getting:
    ReferenceError: Error #1056: Cannot create property dx on bullet.
    at hraSamotna/pohybNaboja()

    And now a few questions:
    1. What does this error actually mean?
    2. If I manage to fix it, will it work the way I want to?

    A huge thanks to whoever will help me.

  2. #2
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    The reference error means that the property dx does not exist as a property or member of the bullet class. The class is not dynamic, so the property dx also cannot be created during runtime.

    After looking at the code, are you really trying to set a bullet's dx property to mouseX - naboje[i].x;? You might actually want to be referencing its x value. the property dx is a part of the hraSamotna class.

    Look at that...
    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    7
    Quote Originally Posted by Baby Minion View Post
    The reference error means that the property dx does not exist as a property or member of the bullet class. The class is not dynamic, so the property dx also cannot be created during runtime.

    After looking at the code, are you really trying to set a bullet's dx property to mouseX - naboje[i].x;? You might actually want to be referencing its x value. the property dx is a part of the hraSamotna class.

    Look at that...
    I've declared the bullet class dynamic and it works now, but I'm getting one of my old problems again. The bullet does not go straight, but follows the mouse. This has to do something with the dx and dy values being renewed every frame, I managed to fix that once, though other problems appeared. I guess I'm back where I was.

    And yes, I want to set the dx and dy as it is, it's a part of the code that makes it shoot where the player aims. I'm not really a fan of trigoniometric functions, but that's how I googled it, and it seems to work.

    Edit: I stopped the mouse following by putting it out of the function that gets executed at frame rate to the strelba() function, but now the speed of the bullet raises with each shot. I traced the speed value, but it remains the same.
    Last edited by Hudacik; 07-20-2009 at 01:33 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center