Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    634

    Public Function Error

    Hi all.

    I am very new to flash so bare with me.

    I have two classes that I am trying to get to intereact, main.as which is the class attached to the stage and then light_mc.as which is attached to a movieclip.

    I have a function in main that I would like to access in light_mc. I have set it up so main stores "this" in a variable called instance so I can easily access the functions within main.

    Here is the code the main.as

    Code:
    package 
    {
    	import flash.utils.Timer;
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Dictionary;
    
    	public class main extends MovieClip
    	{
    		public static var lights:Array = new Array();
    		public static var targets:Array = new Array();
    		public static var lightsList:Array;
    		public static var intervalTimer:Timer;
    		
    		public static var instance:main;
    
    		private var lightPosX:Array = new Array(100,150,200,250,300,350);
    		
    		private var levelIntervals:Array = new Array([1000,2000],[500,1000],[250,750]);
    		public var levelLights:Array = new Array([2000,3000],[1000,1500],[400,750]);
    
    		public function main()
    		{
    			instance = this;
    		}
    
    		public function startGame():void
    		{
    			// Add Lights and Targets to the stage and add them to the arrays
    			for (var i:Number=78.5; i<=471.5; i+=78.5)
    			{
    				targets.push(addToStage(new target_mc(),"target" + String(i+1), i, 204));
    				lights.push(addToStage(new light_mc(),"light" + String(i+1), i ,62));
    			}
    
    			// Create independant copy of lights list for random selection
    			lightsList = lights.concat();
    
    			intervalTimer = new Timer(randLevelInt(1),1);
    			intervalTimer.addEventListener(TimerEvent.TIMER_COMPLETE, pickLight);
    			intervalTimer.start();
    
    		}
    
    		public function pickLight(evt:TimerEvent)
    		{
    			try
    			{
    				var lightChoice:int = randomChoice(lightsList);
    				lightsList[lightChoice].showLight(evt);
    				lightsList.splice(lightChoice,1);
    			}
    			catch (error:TypeError)
    			{
    				// Do Nothing
    				// Prevents error from showing
    			}
    		}
    
    
    		public function addToStage(clip:MovieClip, clipName:String, posX:int, posY:int):MovieClip
    		{
    			stage.addChild(clip);
    			clip.name = clipName;
    			clip.x = posX;
    			clip.y = posY;
    			return clip;
    		}
    		
    		// Random Functions
    		public function randomChoice(options:Array):*
    		{
    			return Math.floor(Math.random()*options.length);
    		}
    		
    					
    		// Random timing functions
    		public function randLevelInt(level:Number):Number
    		{
    			return randRange(levelIntervals[level-1][0],levelIntervals[level-1][1]);
    		}
    		
    		public function randLevelLight(level:int):int
    		{
    			return randRange(levelLights[level-1][0],levelLights[level-1][1]);
    		}
    		
    		public function randRange(minNum:Number, maxNum:Number):Number
    		{
    			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
    			
    		}
    				
    		
    
    	}
    
    }
    This version works, but if I was to move the function randRange up below randomChoices, where I'd like it to go so I can have the two next to each other for organisation purposes it throws up an erorr saying:
    Code:
    D:\Uni\Multimedia Scripting\Game V3\light_mc.as, Line 22	1195: Attempted access of inaccessible method randLevelInt through a reference with static type main.
    Just to clarify the only change I made was to move the position of the function, here is the script that does not work.

    Code:
    package 
    {
    	import flash.utils.Timer;
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Dictionary;
    
    	public class main extends MovieClip
    	{
    		public static var lights:Array = new Array();
    		public static var targets:Array = new Array();
    		public static var lightsList:Array;
    		public static var intervalTimer:Timer;
    		
    		public static var instance:main;
    
    		private var lightPosX:Array = new Array(100,150,200,250,300,350);
    		
    		private var levelIntervals:Array = new Array([1000,2000],[500,1000],[250,750]);
    		public var levelLights:Array = new Array([2000,3000],[1000,1500],[400,750]);
    
    		public function main()
    		{
    			instance = this;
    		}
    
    		public function startGame():void
    		{
    			// Add Lights and Targets to the stage and add them to the arrays
    			for (var i:Number=78.5; i<=471.5; i+=78.5)
    			{
    				targets.push(addToStage(new target_mc(),"target" + String(i+1), i, 204));
    				lights.push(addToStage(new light_mc(),"light" + String(i+1), i ,62));
    			}
    
    			// Create independant copy of lights list for random selection
    			lightsList = lights.concat();
    
    			intervalTimer = new Timer(randLevelInt(1),1);
    			intervalTimer.addEventListener(TimerEvent.TIMER_COMPLETE, pickLight);
    			intervalTimer.start();
    
    		}
    
    		public function pickLight(evt:TimerEvent)
    		{
    			try
    			{
    				var lightChoice:int = randomChoice(lightsList);
    				lightsList[lightChoice].showLight(evt);
    				lightsList.splice(lightChoice,1);
    			}
    			catch (error:TypeError)
    			{
    				// Do Nothing
    				// Prevents error from showing
    			}
    		}
    
    
    		public function addToStage(clip:MovieClip, clipName:String, posX:int, posY:int):MovieClip
    		{
    			stage.addChild(clip);
    			clip.name = clipName;
    			clip.x = posX;
    			clip.y = posY;
    			return clip;
    		}
    		
    		// Random Functions
    		public function randomChoice(options:Array):*
    		{
    			return Math.floor(Math.random()*options.length);
    		}
    		
    		public function randRange(minNum:Number, maxNum:Number):Number
    		{
    			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
    			
    		}		
    							
    		// Random timing functions
    		public function randLevelInt(level:Number):Number
    		{
    			return randRange(levelIntervals[level-1][0],levelIntervals[level-1][1]);
    		}
    		
    		public function randLevelLight(level:int):int
    		{
    			return randRange(levelLights[level-1][0],levelLights[level-1][1]);
    		}
    		
    		
    				
    		
    
    	}
    
    }
    This makes no sense to me what so ever, its nothing major at this point but its bloody annoying, also happens if I move both random Choice and Range functions below the other two.

    Can anyone tell me why?

    Cheers.
    Si vis pacem, para bellum

  2. #2
    Join Date
    Apr 2004
    Location
    Nr London, UK
    Posts
    831
    Class. Names should be camel case so:

    public class Main extends Sprite {

    private static var instance:Main;


    public static function getInstance():Main {
    if(!instance) { instance = new Main(); }
    return instance;
    }

    //constructor
    public function main():void {
    }

    }

  3. #3
    Join Date
    Jul 2005
    Posts
    634
    Thanks for the reply, sadly this doesnt solve my issue.

    I can access several other functions but not the one i need to.

    In another class I am running this command:
    Code:
    trace(Main.getInstance().randLevelInt(1));
    And this throws up this error:
    Code:
    D:\Uni\Multimedia Scripting\Game V3\light_mc.as, Line 22	1195: Attempted access of inaccessible method randLevelInt through a reference with static type Main.
    However if I run this command:
    Code:
    trace(Main.getInstance().randRange(0,10));
    It works fine, so why can it access the one mehtod but not the other they are both public both located in the same class and both being run from same class yet one works and the other does not.

    I edited my Main class as per your sugestions it now looks like this:
    Code:
    package 
    {
    	import flash.utils.Timer;
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Dictionary;
    
    	public class Main extends MovieClip
    	{
    		public static var lights:Array = new Array();
    		public static var targets:Array = new Array();
    		public static var lightsList:Array;
    		public static var intervalTimer:Timer;
    
    		public static var instance:Main;
    
    		private var lightPosX:Array = new Array(100,150,200,250,300,350);
    
    		private var levelIntervals:Array = new Array([1000,2000],[500,1000],[250,750]);
    		public var levelLights:Array = new Array([2000,3000],[1000,1500],[400,750]);
    
    		public function Main()
    		{
    			trace("constructor still works");
    			//instance = this;
    		}
    
    		public static function getInstance():Main
    		{
    			if (! instance)
    			{
    				instance = new Main();
    			}
    			return instance;
    		}
    
    		public function startGame():void
    		{
    			// Add Lights and Targets to the stage and add them to the arrays
    			for (var i:Number=78.5; i<=471.5; i+=78.5)
    			{
    				targets.push(addToStage(new target_mc(),"target" + String(i+1), i, 204));
    				lights.push(addToStage(new light_mc(),"light" + String(i+1), i ,62));
    			}
    
    			// Create independant copy of lights list for random selection
    			lightsList = lights.concat();
    
    			intervalTimer = new Timer(randLevelInt(1),1);
    			intervalTimer.addEventListener(TimerEvent.TIMER_COMPLETE, pickLight);
    			intervalTimer.start();
    
    		}
    
    		public function pickLight(evt:TimerEvent)
    		{
    			try
    			{
    				var lightChoice:int = randomChoice(lightsList);
    				lightsList[lightChoice].showLight(evt);
    				lightsList.splice(lightChoice,1);
    			}
    			catch (error:TypeError)
    			{
    				// Do Nothing
    				// Prevents error from showing
    			}
    		}
    
    
    		public function addToStage(clip:MovieClip, clipName:String, posX:int, posY:int):MovieClip
    		{
    			stage.addChild(clip);
    			clip.name = clipName;
    			clip.x = posX;
    			clip.y = posY;
    			return clip;
    		}
    
    		// Random Functions
    		public function randomChoice(options:Array):*
    		{
    			return Math.floor(Math.random()*options.length);
    		}
    
    		public function randRange(minNum:Number, maxNum:Number):Number
    		{
    			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
    
    		}
    
    		// Random timing functions
    		public function randLevelInt(level:Number):Number
    		{
    			return randRange(levelIntervals[level-1][0],levelIntervals[level-1][1]);
    		}
    
    		public function randLevelLight(level:int):int
    		{
    			return randRange(levelLights[level-1][0],levelLights[level-1][1]);
    		}
    
    
    
    
    
    	}
    
    }
    I changed your code slightly so the constructor is named Main to reflect the change in the class camel case, else the constructor would not automatically run.

    Cheers.
    Last edited by supertom44; 11-21-2010 at 05:52 AM.
    Si vis pacem, para bellum

Posting Permissions

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