dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: AS3 Button help

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    42

    AS3 Button help

    Hey guys,

    Sorry about the title of the thread couldn't think what to put.

    Finishing off the code for my interactive top trumps game ... Basically I have a few layers for code as there's so much and one of the layers 'InGame Script' is code that I only want to run when the start button is pressed. Only problem is that by putting the code into an event listener it throws up all sorts of errors.

    Any ideas?

  2. #2
    Member
    Join Date
    Apr 2011
    Posts
    42
    I basically want to say onto run this code when start button has been pressed.

    Possibly by putting all the InGame code I have into a class and calling the class?
    Last edited by AntBirch; 05-03-2011 at 10:13 AM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That is exactly what an event listener is for.

    Post errors. Post code.

  4. #4
    Member
    Join Date
    Apr 2011
    Posts
    42
    This is the entire InGameScript code

    Code:
    StartButton.addEventListener(MouseEvent.CLICK, StartGame);
    //ContinueButton.addEventListener(MouseEvent.CLICK, ContinueGame);
    
    function StartGame(event:MouseEvent):void
    
    {
    
    var ChosenCards:Array = [AlfaPlay, AscariPlay, AstonOnePlay, AstonRapPlay, AudiPlay, BEngineeringPlay, BentleyPlay, BertonePlay, BristolPlay, BugattiPlay, ChevroletPlay, ChryslerPlay, DauerPlay, DevonPlay, DodgePlay, Ferrari458Play, FerrariEnzPlay, FiskerPlay, FordPlay, GilletPlay, GTAPlay, GumpertApoPlay, GumpertTorPlay, HennesseyPlay, JaguarXJ2Play, JaguarXKRPlay, JossPlay, KeplerPlay, KoenigseggAgePlay, KoenigseggCCXPlay, LamborghiniAvePlay, LamborghiniMurPlay, LanciaPlay, LexusPlay, LotecPlay, LotusPlay, MaseratiGraPlay, MaseratiMC1Play, McLarenF1Play, McLarenMP4Play, MercedesPlay, MoslerPlay, NissanPlay, NoblePlay, PaganiHuaPlay, PaganiZonPlay, Porsche918Play, PorscheCarPlay, RossionPlay, SaleenS5SPlay, SaleenS7Play, SpykerPlay, SSCPlay, SVSPlay, TeslaPlay, TVRPlay, UltimaPlay, VectorAvtPlay, WiesmannPlay, ZenvoPlay];
    
    var b:Array = ChosenCards.sort(CardShuffle); //Setting up an array to shuffle cards
    	
    	function CardShuffle(ChosenCards,b):Number {
    		
    		var num : Number = Math.round(Math.random()*2)-1;
    	
    		return num;
    	}
    
    var PlayerCards:Array = []; //Sets up an array to hold the players cards.
    
    var AICards:Array = []; //Same as above but for the ai's cards
    	
    for (var p:int = 0; p <60; p++) { //Select 30 of the cards available.
    	
    	var TargetHand:Array; //An array containing all cards.
    	
    	if (p % 2 == 0){ //Gives half of the cards to the player.
    		
    		TargetHand = PlayerCards;
    		
    		}else{ //and then give the other half to the ai.
    			
    		TargetHand = AICards;
    		
    	}
    	
    	TargetHand.push(ChosenCards[p]);
    	
    	}
    
    ContinueButton.visible = false;
    
    	trace("Players 30 random cards\n");
    	for(var s:int = 0; s < PlayerCards.length; s++)
    	{
    		trace((s + 1) + ". " + PlayerCards[s].name)
    		//Problem could be from this piece of code.
    	}
    	
    	trace("\nAI 30 random cards\n");
    	for(var t:int = 0; t < AICards.length; t++)
    	{
    		trace((t + 1) + ". " + AICards[t].name)
    		
    	}
    	
    	function PickRandomCard(max:int):Number
    	{
    		
    		var pick:Number = new Number(Math.round(Math.random() * max));
    		
    		return(pick);
    		
    	}
    
    	var PickedPlayerCard:String = new String();
    	var PickedAICard:String = new String();
    	
    	PickedPlayerCard = PlayerCards[PickRandomCard(15)].name;
    	PickedAICard = AICards[PickRandomCard(15)].name;
    	
    	trace("\nPlayer First Card: " + PickedPlayerCard);
    	trace("AI First Card: " + PickedAICard);
    	
    	this[PickedPlayerCard].visible = true; //Making chosen card visible.
    	this[PickedPlayerCard].x = 75; //Positioning chosen card.
    	this[PickedAICard].visible = true;
    	this[PickedAICard].x = 430;
    	
    	StartButton.visible = false;
    	BackButton.visible = false;
    	
    	SpeedButton.x = 99; //Setting position of card buttons so player can choose a value.
    	ZerotoSixtyButton.x = 99;
    	PowerButton.x = 99;
    	WeightButton.x = 99;
    	CostButton.x = 99;
    	
    	SpeedButton.buttonMode = true;
    	ZerotoSixtyButton.buttonMode = true;
    	PowerButton.buttonMode = true;
    	WeightButton.buttonMode = true;
    	CostButton.buttonMode = true;
    	
    	SpeedButton.visible = true;
    	ZerotoSixtyButton.visible = true;
    	PowerButton.visible = true;
    	WeightButton.visible = true;
    	CostButton.visible = true;
    	
    	SpeedButton.addEventListener(MouseEvent.CLICK, SelectSpeed); //Waits for chosen buttons to be pressed.
    	ZerotoSixtyButton.addEventListener(MouseEvent.CLICK, SelectZerotoSixty);
    	PowerButton.addEventListener(MouseEvent.CLICK, SelectPower);
    	WeightButton.addEventListener(MouseEvent.CLICK, SelectWeight);
    	CostButton.addEventListener(MouseEvent.CLICK, SelectCost);
    	
    	function SelectSpeed(event:MouseEvent):void //If this button is clicked.
    
    	{
    		if(this[PickedPlayerCard].visible){ //Checks if this is visible.
    			
    			if(this[PickedPlayerCard].Speed > this[PickedAICard].Speed) //Is this chosen value is larger and all of above is true ...
    			
    			{
    				
    				PlayerWinner.visible = true; //Do this.
    				AILoser.visible = true;
    		
    				} else if (this[PickedPlayerCard].Speed < this[PickedAICard].Speed){ //If not and this is true do this.
    			
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    				
    				} else { //Else if neither are true do this.
    					
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    				
    			}
    		}
    	}
    	
    	function SelectZerotoSixty(event:MouseEvent):void
    
    	{
    		if(this[PickedPlayerCard].visible){
    			
    			if(this[PickedPlayerCard].ZerotoSixty < this[PickedAICard].ZerotoSixty) //Notice that this statement is the same as above except we're now asking if this this chosen value is smaller instead of larger.
    			
    			{
    				
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    		
    				} else if (this[PickedPlayerCard].ZerotoSixty > this[PickedAICard].ZerotoSixty){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    				
    				} else {
    					
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    				
    			}
    		}
    	}
    	
    	function SelectPower(event:MouseEvent):void
    
    	{
    		if(this[PickedPlayerCard].visible){
    			
    			if(this[PickedPlayerCard].Power > this[PickedAICard].Power)
    			
    			{
    				
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    		
    				} else if (this[PickedPlayerCard].Power < this[PickedAICard].Power){
    			
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    				
    				} else {
    					
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    				
    			}
    		}
    	}
    	
    	function SelectWeight(event:MouseEvent):void
    
    	{
    		if(this[PickedPlayerCard].visible){
    			
    			if(this[PickedPlayerCard].Weight < this[PickedAICard].Weight)
    			
    			{
    				
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    		
    				} else if (this[PickedPlayerCard].Weight > this[PickedAICard].Weight){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    				
    				} else {
    					
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    				
    			}
    		}
    	}
    	
    	function SelectCost(event:MouseEvent):void
    
    	{
    		if(this[PickedPlayerCard].visible){
    			
    			if(this[PickedPlayerCard].Cost > this[PickedAICard].Cost)
    			
    			{
    				
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    		
    				} else if (this[PickedPlayerCard].Cost < this[PickedAICard].Cost){
    			
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    				
    				} else {
    					
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    				
    			}
    		}
    	}
    	
    	setChildIndex(SpeedButton,numChildren - 1); //Sets this button to the front of the scene.
    	setChildIndex(ZerotoSixtyButton,numChildren - 1);
    	setChildIndex(PowerButton,numChildren - 1);
    	setChildIndex(WeightButton,numChildren - 1);
    	setChildIndex(CostButton,numChildren - 1);
    	
    }
    The code works fine and the start button makes the first 2 cards appear until I click the value selection button (either SpeedButton or so on) and then he code gives this error: TypeError: Error #1010: A term is undefined and has no properties.
    at Function/<anonymous>()

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your indentation obscures the structure, but it looks like that entire block of code other than the first line is inside the StartGame function. Try not to define functions inside other functions. And be aware that anything you declare inside a function is scoped to that function and will not be defined outside it.

    Fix your indentation so you can tell what code is inside other code structures such as functions and loops. You have a scope problem somewhere, and random indentation is not helping you find it.

    There's no reason I see to set PickedPlayerCard and PickedAICard to the NAMES of the selected cards. Make those variables the actual selected cards. Then you can get replace all the "this[PickedPlayerCard]" with just PickedPlayerCard, and similarly for PickedAICard. Doing that may actually "fix" your error, if the value of "this" is messed up by your nested functions.

    To bring something to the front, you don't need to use setChildIndex. You can just use addChild, which will put it at the top. I'm not sure why you need to do that at all, since no other code puts anything on the displaylist or changes the child indexes, so nothing would have moved those from wherever you put them to start with.

    Your variable names should still begin with lowercase letters.

  6. #6
    Member
    Join Date
    Apr 2011
    Posts
    42
    Hey thanks for quick reply,

    The indentations have been sorted I honestly missed that I hadn't sorted that.

    I don't see any way of putting the selection buttons (SpeedSelection) outside the start button event listener as it doesn't find it.

    I also don't understand what you mean about being able to replace this[PickedPlayerCard] with just PickedPlayerCard?

    Also removed setChildIndex I didn't need it I understand why I don't either too thank you.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Post the fixed indentation version. You never declare the variables for SpeedSelection, etc so I assumed they were IDE-placed instances.

    About PickedPlayerCard and PickedAICard, what you are doing is storing the cards' names, then later looking the cards up again by those names. Since all you want is the cards in the first place, just keep the reference to the cards themselves.

  8. #8
    Member
    Join Date
    Apr 2011
    Posts
    42
    Code:
    StartButton.addEventListener(MouseEvent.CLICK, StartGame);
    //ContinueButton.addEventListener(MouseEvent.CLICK, ContinueGame);
    
    function StartGame(event:MouseEvent):void
    
    {
    
    	var ChosenCards:Array = [AlfaPlay, AscariPlay, AstonOnePlay, AstonRapPlay, AudiPlay, BEngineeringPlay, BentleyPlay, BertonePlay, BristolPlay, BugattiPlay, ChevroletPlay, ChryslerPlay, DauerPlay, DevonPlay, DodgePlay, Ferrari458Play, FerrariEnzPlay, FiskerPlay, FordPlay, GilletPlay, GTAPlay, GumpertApoPlay, GumpertTorPlay, HennesseyPlay, JaguarXJ2Play, JaguarXKRPlay, JossPlay, KeplerPlay, KoenigseggAgePlay, KoenigseggCCXPlay, LamborghiniAvePlay, LamborghiniMurPlay, LanciaPlay, LexusPlay, LotecPlay, LotusPlay, MaseratiGraPlay, MaseratiMC1Play, McLarenF1Play, McLarenMP4Play, MercedesPlay, MoslerPlay, NissanPlay, NoblePlay, PaganiHuaPlay, PaganiZonPlay, Porsche918Play, PorscheCarPlay, RossionPlay, SaleenS5SPlay, SaleenS7Play, SpykerPlay, SSCPlay, SVSPlay, TeslaPlay, TVRPlay, UltimaPlay, VectorAvtPlay, WiesmannPlay, ZenvoPlay];
    	
    	var b:Array = ChosenCards.sort(CardShuffle); //Setting up an array to shuffle cards
    		
    		function CardShuffle(ChosenCards,b):Number {
    			
    			var num : Number = Math.round(Math.random()*2)-1;
    		
    			return num;
    	}
    	
    	var PlayerCards:Array = []; //Sets up an array to hold the players cards.
    	
    	var AICards:Array = []; //Same as above but for the ai's cards
    		
    	for (var p:int = 0; p <60; p++) { //Select 30 of the cards available.
    		
    		var TargetHand:Array; //An array containing all cards.
    		
    		if (p % 2 == 0){ //Gives half of the cards to the player.
    			
    			TargetHand = PlayerCards;
    			
    			}else{ //and then give the other half to the ai.
    				
    			TargetHand = AICards;
    			
    		}
    		
    		TargetHand.push(ChosenCards[p]);
    		
    		}
    	
    	ContinueButton.visible = false;
    	
    		trace("Players 30 random cards\n");
    		for(var s:int = 0; s < PlayerCards.length; s++)
    		{
    			trace((s + 1) + ". " + PlayerCards[s].name)
    			//Problem could be from this piece of code.
    		}
    		
    		trace("\nAI 30 random cards\n");
    		for(var t:int = 0; t < AICards.length; t++)
    		{
    			trace((t + 1) + ". " + AICards[t].name)
    			
    		}
    		
    		function PickRandomCard(max:int):Number
    		{
    			
    			var pick:Number = new Number(Math.round(Math.random() * max));
    			
    			return(pick);
    			
    		}
    	
    		var PickedPlayerCard:String = new String();
    		var PickedAICard:String = new String();
    		
    		PickedPlayerCard = PlayerCards[PickRandomCard(15)].name;
    		PickedAICard = AICards[PickRandomCard(15)].name;
    		
    		trace("\nPlayer First Card: " + PickedPlayerCard);
    		trace("AI First Card: " + PickedAICard);
    		
    		this[PickedPlayerCard].visible = true; //Making chosen card visible.
    		this[PickedPlayerCard].x = 75; //Positioning chosen card.
    		this[PickedAICard].visible = true;
    		this[PickedAICard].x = 430;
    		
    		StartButton.visible = false;
    		BackButton.visible = false;
    		
    		SpeedButton.x = 99; //Setting position of card buttons so player can choose a value.
    		ZerotoSixtyButton.x = 99;
    		PowerButton.x = 99;
    		WeightButton.x = 99;
    		CostButton.x = 99;
    		
    		SpeedButton.buttonMode = true;
    		ZerotoSixtyButton.buttonMode = true;
    		PowerButton.buttonMode = true;
    		WeightButton.buttonMode = true;
    		CostButton.buttonMode = true;
    		
    		SpeedButton.visible = true;
    		ZerotoSixtyButton.visible = true;
    		PowerButton.visible = true;
    		WeightButton.visible = true;
    		CostButton.visible = true;
    	
    		SpeedButton.addEventListener(MouseEvent.CLICK, SelectSpeed); //Waits for chosen buttons to be pressed.
    		ZerotoSixtyButton.addEventListener(MouseEvent.CLICK, SelectZerotoSixty);
    		PowerButton.addEventListener(MouseEvent.CLICK, SelectPower);
    		WeightButton.addEventListener(MouseEvent.CLICK, SelectWeight);
    		CostButton.addEventListener(MouseEvent.CLICK, SelectCost);
    		
    		function SelectSpeed(event:MouseEvent):void //If this button is clicked.
    	
    		{
    			if(this[PickedPlayerCard].visible){ //Checks if this is visible.
    				
    				if(this[PickedPlayerCard].Speed > this[PickedAICard].Speed) //Is this chosen value is larger and all of above is true ...
    				
    				{
    					
    					PlayerWinner.visible = true; //Do this.
    					AILoser.visible = true;
    			
    					} else if (this[PickedPlayerCard].Speed < this[PickedAICard].Speed){ //If not and this is true do this.
    				
    					PlayerLoser.visible = true;
    					AIWinner.visible = true;
    					
    					} else { //Else if neither are true do this.
    						
    					PlayerDraw.visible = true;
    					AIDraw.visible = true;
    					
    				}
    			}
    		}
    		
    		function SelectZerotoSixty(event:MouseEvent):void
    	
    		{
    			if(this[PickedPlayerCard].visible){
    				
    				if(this[PickedPlayerCard].ZerotoSixty < this[PickedAICard].ZerotoSixty) //Notice that this statement is the same as above except we're now asking if this this chosen value is smaller instead of larger.
    				
    				{
    					
    					PlayerWinner.visible = true;
    					AILoser.visible = true;
    			
    					} else if (this[PickedPlayerCard].ZerotoSixty > this[PickedAICard].ZerotoSixty){
    					PlayerLoser.visible = true;
    					AIWinner.visible = true;
    					
    					} else {
    						
    					PlayerDraw.visible = true;
    					AIDraw.visible = true;
    					
    				}
    			}
    		}
    		
    		function SelectPower(event:MouseEvent):void
    	
    		{
    			if(this[PickedPlayerCard].visible){
    				
    				if(this[PickedPlayerCard].Power > this[PickedAICard].Power)
    				
    				{
    					
    					PlayerWinner.visible = true;
    					AILoser.visible = true;
    			
    					} else if (this[PickedPlayerCard].Power < this[PickedAICard].Power){
    				
    					PlayerLoser.visible = true;
    					AIWinner.visible = true;
    					
    					} else {
    						
    					PlayerDraw.visible = true;
    					AIDraw.visible = true;
    					
    				}
    			}
    		}
    		
    		function SelectWeight(event:MouseEvent):void
    	
    		{
    			if(this[PickedPlayerCard].visible){
    				
    				if(this[PickedPlayerCard].Weight < this[PickedAICard].Weight)
    				
    				{
    					
    					PlayerWinner.visible = true;
    					AILoser.visible = true;
    			
    					} else if (this[PickedPlayerCard].Weight > this[PickedAICard].Weight){
    					PlayerLoser.visible = true;
    					AIWinner.visible = true;
    					
    					} else {
    						
    					PlayerDraw.visible = true;
    					AIDraw.visible = true;
    					
    				}
    			}
    		}
    		
    		function SelectCost(event:MouseEvent):void
    	
    		{
    			if(this[PickedPlayerCard].visible){
    				
    				if(this[PickedPlayerCard].Cost > this[PickedAICard].Cost)
    				
    				{
    					
    					PlayerWinner.visible = true;
    					AILoser.visible = true;
    			
    					} else if (this[PickedPlayerCard].Cost < this[PickedAICard].Cost){
    				
    					PlayerLoser.visible = true;
    					AIWinner.visible = true;
    					
    					} else {
    						
    					PlayerDraw.visible = true;
    					AIDraw.visible = true;
    				
    					}
    				}
    		}
    }
    The code above is the fixed indentation code.

    Code:
    AlfaPlay.Speed = 181; //Sets values under the specified names to the following movie clips.
    AlfaPlay.ZerotoSixty = 41;
    AlfaPlay.Power = 450;
    AlfaPlay.Weight = 1585;
    AlfaPlay.Cost = 227000;
    
    AscariPlay.Speed = 220;
    AscariPlay.ZerotoSixty = 28;
    AscariPlay.Power = 600;
    AscariPlay.Weight = 1280;
    AscariPlay.Cost = 600000;
    All the cards are assigned values like the code above shows.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You changed a bit of indendation, but there's a lot left. Let me show you.
    Code:
    StartButton.addEventListener(MouseEvent.CLICK, StartGame);
    //ContinueButton.addEventListener(MouseEvent.CLICK, ContinueGame);
    
    function StartGame(event:MouseEvent):void{
    
    	var ChosenCards:Array = [AlfaPlay, AscariPlay, AstonOnePlay, AstonRapPlay, AudiPlay, BEngineeringPlay, BentleyPlay, BertonePlay, BristolPlay, BugattiPlay, ChevroletPlay, ChryslerPlay, DauerPlay, DevonPlay, DodgePlay, Ferrari458Play, FerrariEnzPlay, FiskerPlay, FordPlay, GilletPlay, GTAPlay, GumpertApoPlay, GumpertTorPlay, HennesseyPlay, JaguarXJ2Play, JaguarXKRPlay, JossPlay, KeplerPlay, KoenigseggAgePlay, KoenigseggCCXPlay, LamborghiniAvePlay, LamborghiniMurPlay, LanciaPlay, LexusPlay, LotecPlay, LotusPlay, MaseratiGraPlay, MaseratiMC1Play, McLarenF1Play, McLarenMP4Play, MercedesPlay, MoslerPlay, NissanPlay, NoblePlay, PaganiHuaPlay, PaganiZonPlay, Porsche918Play, PorscheCarPlay, RossionPlay, SaleenS5SPlay, SaleenS7Play, SpykerPlay, SSCPlay, SVSPlay, TeslaPlay, TVRPlay, UltimaPlay, VectorAvtPlay, WiesmannPlay, ZenvoPlay];
    	
    	var b:Array = ChosenCards.sort(CardShuffle); //Setting up an array to shuffle cards
    		
    	function CardShuffle(ChosenCards,b):Number {
    		var num : Number = Math.round(Math.random()*2)-1;
    		return num;
    	}
    	
    	var PlayerCards:Array = []; //Sets up an array to hold the players cards.
    	
    	var AICards:Array = []; //Same as above but for the ai's cards
    		
    	for (var p:int = 0; p <60; p++) { //Select 30 of the cards available.
    		var TargetHand:Array; //An array containing all cards.
    		if (p % 2 == 0){ //Gives half of the cards to the player.
    			TargetHand = PlayerCards;
    		}else{ //and then give the other half to the ai.
    			TargetHand = AICards;
    		}
    		TargetHand.push(ChosenCards[p]);
    	}
    	
    	ContinueButton.visible = false;
    	trace("Players 30 random cards\n");
    	for(var s:int = 0; s < PlayerCards.length; s++){
    		trace((s + 1) + ". " + PlayerCards[s].name)
    		//Problem could be from this piece of code.
    	}
    		
    	trace("\nAI 30 random cards\n");
    	for(var t:int = 0; t < AICards.length; t++){
    		trace((t + 1) + ". " + AICards[t].name)
    	}
    
    	function PickRandomCard(max:int):Number	{
    		var pick:Number = new Number(Math.round(Math.random() * max));
    		return pick;
    	}
    	
    	var PickedPlayerCard:String = new String();
    	var PickedAICard:String = new String();
    		
    	PickedPlayerCard = PlayerCards[PickRandomCard(15)].name;
    	PickedAICard = AICards[PickRandomCard(15)].name;
    		
    	trace("\nPlayer First Card: " + PickedPlayerCard);
    	trace("AI First Card: " + PickedAICard);
    		
    	this[PickedPlayerCard].visible = true; //Making chosen card visible.
    	this[PickedPlayerCard].x = 75; //Positioning chosen card.
    	this[PickedAICard].visible = true;
    	this[PickedAICard].x = 430;
    		
    	StartButton.visible = false;
    	BackButton.visible = false;
    		
    	SpeedButton.x = 99; //Setting position of card buttons so player can choose a value.
    	ZerotoSixtyButton.x = 99;
    	PowerButton.x = 99;
    	WeightButton.x = 99;
    	CostButton.x = 99;
    		
    	SpeedButton.buttonMode = true;
    	ZerotoSixtyButton.buttonMode = true;
    	PowerButton.buttonMode = true;
    	WeightButton.buttonMode = true;
    	CostButton.buttonMode = true;
    		
    	SpeedButton.visible = true;
    	ZerotoSixtyButton.visible = true;
    	PowerButton.visible = true;
    	WeightButton.visible = true;
    	CostButton.visible = true;
    
    	SpeedButton.addEventListener(MouseEvent.CLICK, SelectSpeed); //Waits for chosen buttons to be pressed.
    	ZerotoSixtyButton.addEventListener(MouseEvent.CLICK, SelectZerotoSixty);
    	PowerButton.addEventListener(MouseEvent.CLICK, SelectPower);
    	WeightButton.addEventListener(MouseEvent.CLICK, SelectWeight);
    	CostButton.addEventListener(MouseEvent.CLICK, SelectCost);
    
    	//If this button is clicked.	
    	function SelectSpeed(event:MouseEvent):void{
    		if(this[PickedPlayerCard].visible){ //Checks if this is visible.
    			if(this[PickedPlayerCard].Speed > this[PickedAICard].Speed)	{
    				//Is this chosen value is larger and all of above is true ...							PlayerWinner.visible = true; //Do this.
    				AILoser.visible = true;
    			} else if (this[PickedPlayerCard].Speed < this[PickedAICard].Speed){
    				//If not and this is true do this.
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    			} else { //Else if neither are true do this.
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    			}
    		}
    	}
    		
    	function SelectZerotoSixty(event:MouseEvent):void{
    		if(this[PickedPlayerCard].visible){
    			if(this[PickedPlayerCard].ZerotoSixty < this[PickedAICard].ZerotoSixty){
    			//Notice that this statement is the same as above except we're now asking if this this chosen value is smaller instead of larger.
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    			} else if (this[PickedPlayerCard].ZerotoSixty > this[PickedAICard].ZerotoSixty){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    			} else {
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    			}
    		}
    	}
    		
    	function SelectPower(event:MouseEvent):void{
    		if(this[PickedPlayerCard].visible){
    			if(this[PickedPlayerCard].Power > this[PickedAICard].Power){
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    			} else if (this[PickedPlayerCard].Power < this[PickedAICard].Power){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    			} else {
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    			}
    		}
    	}
    		
    	function SelectWeight(event:MouseEvent):void{
    		if(this[PickedPlayerCard].visible){
    			if(this[PickedPlayerCard].Weight < this[PickedAICard].Weight){
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    			} else if (this[PickedPlayerCard].Weight > this[PickedAICard].Weight){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    			} else {
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    			}
    		}
    	}
    		
    	function SelectCost(event:MouseEvent):void{
    		if(this[PickedPlayerCard].visible){
    			if(this[PickedPlayerCard].Cost > this[PickedAICard].Cost){
    				PlayerWinner.visible = true;
    				AILoser.visible = true;
    			} else if (this[PickedPlayerCard].Cost < this[PickedAICard].Cost){
    				PlayerLoser.visible = true;
    				AIWinner.visible = true;
    			} else {
    				PlayerDraw.visible = true;
    				AIDraw.visible = true;
    			}
    		}
    	}
    }
    That's fixed indentation. I didn't fix anything else yet.

    See how the entire thing is inside StartGame? That's bad. The only thing StartGame should do is start the game. All your utility functions like CardShuffle and PickRandomCard should be defined outside StartGame, as should any variable which should be in a scope larger than the game starting actions. All your select functions should be outside StartGame.
    Last edited by 5TonsOfFlax; 05-03-2011 at 12:00 PM.

  10. #10
    Member
    Join Date
    Apr 2011
    Posts
    42
    Thanks for going over that.

    I'm struggling to work out which part of the code actual needs to happen to start the game ... whenever I put some of the code into the startgame function it always brings up an error :

    TypeError: Error #1010: A term is undefined and has no properties.
    at TopTrumpsGamev24_fla::MainTimeline/frame1()

  11. #11
    Member
    Join Date
    Apr 2011
    Posts
    42
    I think I've got it now

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    StartButton.addEventListener(MouseEvent.CLICK, startGame);
    //ContinueButton.addEventListener(MouseEvent.CLICK, ContinueGame);
    
    var chosenCards:Array = [AlfaPlay, AscariPlay, AstonOnePlay, AstonRapPlay, AudiPlay, BEngineeringPlay, BentleyPlay, BertonePlay, BristolPlay, BugattiPlay, ChevroletPlay, ChryslerPlay, DauerPlay, DevonPlay, DodgePlay, Ferrari458Play, FerrariEnzPlay, FiskerPlay, FordPlay, GilletPlay, GTAPlay, GumpertApoPlay, GumpertTorPlay, HennesseyPlay, JaguarXJ2Play, JaguarXKRPlay, JossPlay, KeplerPlay, KoenigseggAgePlay, KoenigseggCCXPlay, LamborghiniAvePlay, LamborghiniMurPlay, LanciaPlay, LexusPlay, LotecPlay, LotusPlay, MaseratiGraPlay, MaseratiMC1Play, McLarenF1Play, McLarenMP4Play, MercedesPlay, MoslerPlay, NissanPlay, NoblePlay, PaganiHuaPlay, PaganiZonPlay, Porsche918Play, PorscheCarPlay, RossionPlay, SaleenS5SPlay, SaleenS7Play, SpykerPlay, SSCPlay, SVSPlay, TeslaPlay, TVRPlay, UltimaPlay, VectorAvtPlay, WiesmannPlay, ZenvoPlay];
    	
    var playerCards:Array;
    var aiCards:Array;
    
    var pickedPlayerCard:MovieClip;
    var pickedAICard:MovieClip;
    
    	
    function cardShuffle(chosenCards,b):Number {
    	var num : Number = Math.round(Math.random()*2)-1;
    	return num;
    }
    
    function pickRandomCard(max:int):Number	{
    	var pick:Number = new Number(Math.round(Math.random() * max));
    	return pick;
    }
    
    
    function startGame(event:MouseEvent):void{
    
    	
    		
    	ContinueButton.visible = false;
    	StartButton.visible = false;
    	BackButton.visible = false;
    		
    	SpeedButton.x = 99; //Setting position of card buttons so player can choose a value.
    	ZerotoSixtyButton.x = 99;
    	PowerButton.x = 99;
    	WeightButton.x = 99;
    	CostButton.x = 99;
    		
    	SpeedButton.buttonMode = true;
    	ZerotoSixtyButton.buttonMode = true;
    	PowerButton.buttonMode = true;
    	WeightButton.buttonMode = true;
    	CostButton.buttonMode = true;
    		
    	SpeedButton.visible = true;
    	ZerotoSixtyButton.visible = true;
    	PowerButton.visible = true;
    	WeightButton.visible = true;
    	CostButton.visible = true;
    
    	SpeedButton.addEventListener(MouseEvent.CLICK, selectSpeed); //Waits for chosen buttons to be pressed.
    	ZerotoSixtyButton.addEventListener(MouseEvent.CLICK, selectZerotoSixty);
    	PowerButton.addEventListener(MouseEvent.CLICK, selectPower);
    	WeightButton.addEventListener(MouseEvent.CLICK, selectWeight);
    	CostButton.addEventListener(MouseEvent.CLICK, selectCost);
    
    	dealCards();
    	
    	pickedPlayerCard = getACard(playerCards);
    	pickedAICard = getACard(aiCards);
    	
    	trace("\nPlayer First Card: " + pickedPlayerCard.name);
    	trace("AI First Card: " + pickedAICard.name);
    		
    	pickedPlayerCard.visible = true; //Making chosen card visible.
    	pickedPlayerCard.x = 75; //Positioning chosen card.
    	pickedAICard.visible = true;
    	pickedAICard.x = 430;
    	
    }
    
    function dealCards():void{
    	playerCards = []; //Sets up an array to hold the players cards.
    	
    	aiCards = []; //Same as above but for the ai's cards
    	chosenCards.sort(cardShuffle); //Setting up an array to shuffle cards
    	for (var p:int = 0; p <chosenCards.length; p++) { //Select 30 of the cards available.
    		var targetHand:Array; //An array containing all cards.
    		if (p % 2 == 0){ //Gives half of the cards to the player.
    			targetHand = playerCards;
    		}else{ //and then give the other half to the ai.
    			targetHand = aiCards;
    		}
    		targetHand.push(chosenCards[p]);
    	}
    	
    	trace("Players 30 random cards\n");
    	for(var s:int = 0; s < playerCards.length; s++){
    		trace((s + 1) + ". " + playerCards[s].name)
    	}
    		
    	trace("\nAI 30 random cards\n");
    	for(var t:int = 0; t < aiCards.length; t++){
    		trace((t + 1) + ". " + aiCards[t].name)
    	}
    }
    
    function getACard(hand:Array):MovieClip{
    	return hand[pickRandomCard(15)];
    }
    
    function selectSpeed(event:MouseEvent):void{
    	if(pickedPlayerCard.visible){ //Checks if this is visible.
    		if(pickedPlayerCard.Speed > pickedAICard.Speed)	{
    			//Is this chosen value is larger and all of above is true ...							PlayerWinner.visible = true; //Do this.
    			AILoser.visible = true;
    		} else if (pickedPlayerCard.Speed < pickedAICard.Speed){
    			//If not and this is true do this.
    			PlayerLoser.visible = true;
    			AIWinner.visible = true;
    		} else { //Else if neither are true do this.
    			PlayerDraw.visible = true;
    			AIDraw.visible = true;
    		}
    	}
    }
    	
    function selectZerotoSixty(event:MouseEvent):void{
    	if(pickedPlayerCard.visible){
    		if(pickedPlayerCard.ZerotoSixty < pickedAICard.ZerotoSixty){
    		//Notice that this statement is the same as above except we're now asking if this this chosen value is smaller instead of larger.
    			PlayerWinner.visible = true;
    			AILoser.visible = true;
    		} else if (pickedPlayerCard.ZerotoSixty > pickedAICard.ZerotoSixty){
    			PlayerLoser.visible = true;
    			AIWinner.visible = true;
    		} else {
    			PlayerDraw.visible = true;
    			AIDraw.visible = true;
    		}
    	}
    }
    	
    function selectPower(event:MouseEvent):void{
    	if(pickedPlayerCard.visible){
    		if(pickedPlayerCard.Power > pickedAICard.Power){
    			PlayerWinner.visible = true;
    			AILoser.visible = true;
    		} else if (pickedPlayerCard.Power < pickedAICard.Power){
    			PlayerLoser.visible = true;
    			AIWinner.visible = true;
    		} else {
    			PlayerDraw.visible = true;
    			AIDraw.visible = true;
    		}
    	}
    }
    	
    function selectWeight(event:MouseEvent):void{
    	if(pickedPlayerCard.visible){
    		if(pickedPlayerCard.Weight < pickedAICard.Weight){
    			PlayerWinner.visible = true;
    			AILoser.visible = true;
    		} else if (pickedPlayerCard.Weight > pickedAICard.Weight){
    			PlayerLoser.visible = true;
    			AIWinner.visible = true;
    		} else {
    			PlayerDraw.visible = true;
    			AIDraw.visible = true;
    		}
    	}
    }
    
    function selectCost(event:MouseEvent):void{
    	if(pickedPlayerCard.visible){
    		if(pickedPlayerCard.Cost > pickedAICard.Cost){
    			PlayerWinner.visible = true;
    			AILoser.visible = true;
    		} else if (pickedPlayerCard.Cost < pickedAICard.Cost){
    			PlayerLoser.visible = true;
    			AIWinner.visible = true;
    		} else {
    			PlayerDraw.visible = true;
    			AIDraw.visible = true;
    		}
    	}
    }
    I think you still need to distinguish between starting a game and starting a hand, or round.

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