AS 2 is my PC haunted ?
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: is my PC haunted ?

  1. #1
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309

    is my PC haunted ?

    Hi, all think I've done is assign a variable name to make it easier to refer to in my card game and this code traces the cards perfectly but not it's length. I am still not absolutely sure of the second's value in the aTemp array, but this doesn't make sense to me. Can anyone explain to me what's different in the showHands function?

    Code:
    var aSuits:Array = ["oros", "copes", "espases", "bastos"];
    var aValues:Array = ["Manilla", "As", "Rei", "Cavall", "Sota", "8", "7", "6", "5", "4", "3", "2"];
    var aTemp:Array = new Array();
    var aDeck:Array = new Array();
    var playerHand:Array = new Array();
    
    createDeck();
    
    function createDeck():Void
    {
    	for (i = 0; i < 4; i++)
    	{
    		for (j = 0; j < 12; j++)
    		{
    			//var oCard:Object = {sCardSuit:[i], sCardNumber:[j]};
    			var oCard:Object = new Object();
    			oCard.sCardSuit = i;
    			oCard.sCardNumber = j;
    
    			aTemp.push(oCard);
    		}
    	}
    	randomizeDeck();
    }
    
    function randomizeDeck():Void
    {
    	while (aTemp.length > 0)
    	{
    		var r:Number = Math.floor(Math.random() * aTemp.length);
    		aDeck.push(aTemp[r]);
    		aTemp.splice(r,1);
    	}
    	createHands();
    }
    
    function createHands():Void
    {
    	for (i = 0; i < aDeck.length; i++)
    	{
    		aTemp[i] = new Array();
    		aTemp[i].push(aDeck.splice(0, aValues.length));
    		trace("Player: " + (i + 1) + " - " + aTemp[i]);
    		aTemp[i][0].sortOn(["sCardSuit", "sCardNumber"],[Array.NUMERIC, Array.NUMERIC]);
    	}
    	showHands();
    }
    
    function showHands():Void
    {
    	for (i = 0; i < aSuits.length; i++)
    	{
    		trace("Player: " + (i + 1));
    		
    		for (j = 0; j < aValues.length; j++)
    		{
    			playerHand[i] = aValues[aTemp[i][0][j].sCardNumber] + " - " + aSuits[aTemp[i][0][j].sCardSuit];
    			trace("\t" + playerHand[i]);
    		}
    		trace(playerHand[i].length + " cards");
    		trace("----------");
    	}
    }
    Last edited by capdetrons; 03-17-2014 at 12:23 PM.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    969
    Hi Cap,

    I believe you are mixing the arrays up, even though you declare them to be new arrays (aTemp especially), try this then compare them
    PHP Code:
    stop();

    var 
    aSuits:Array = ["oros""copes""espases""bastos"];
    var 
    aValues:Array = ["Manilla""As""Rei""Cavall""Sota""8""7""6""5""4""3""2"];
    var 
    aTemp:Array = new Array();
    var 
    aDeck:Array = new Array();
    var 
    playerHand:Array = new Array();
    var 
    i:Numberj:Numbershuffle:Number;

    createDeck();

    function 
    createDeck():Void
    {
        for (
    0aSuits.lengthi++)
        {
            for (
    0aValues.lengthj++)
            {
                var 
    oCard:Object = {sCardNumber:aValues[j], sCardSuit:aSuits[i]};
                
    aTemp.push(oCard);
            }
        }
        
    randomizeCards();
    }

    function 
    randomizeCards():Void
    {
        while (
    aTemp.length 0)
        {
            var 
    r:Number Math.floor(Math.random() * aTemp.length);
            
    aDeck.push(aTemp[r]);
            
    aTemp.splice(r,1);
        }
        
    createHands();
    }

    function 
    createHands():Void
    {
        for (
    0aDeck.lengthi++)
        {
            
    playerHand[i] = [];
            
    playerHand[i].push(aDeck.splice(0aValues.length));
            
    playerHand[i][0].sortOn(['sCardSuit''sCardNumber'],[Array.NUMERIC, Array.NUMERIC]);
        }
        
    showHands();
    }

    function 
    showHands():Void
    {
        for (
    0aSuits.lengthi++)
        {
            
    trace("Player: " + (1));
            for (
    0aValues.lengthj++)
            {
                
    trace("\t" playerHand[i][0][j].sCardNumber " - " playerHand[i][0][j].sCardSuit);
            }
            
    trace(playerHand[i][0].length " cards");
            
    trace("----------");
        }


  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Yes your pc is haunted by some spanish speaking ghost. It's messed with the card suits and values.

    I made a working fla out of the code I had. It's in AS3 CC and I can't publish as2. But the code is all in one place so it shouldn't be hard to convert.

    Attachment 74769
    Last edited by moot; 03-19-2014 at 03:34 PM.

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Here are source files.

    flashkit_moot_example.zip

  5. #5
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    Hi, thanks, Moot,

    great work. I appreciate you going through the trouble. I will keep it and try learning from it even though it's too much for me. I'm not even sure about two dimensional arrays or multidimensional...

    I had to think my PC is haunted, have you traced out the post I wrote? I believe FruitBeard when he comments I'm mixing up arrays, but I still think the playerHand array "holder" should trace the same length than the card hands. When I changed the same code and added an extra card for each suit and used the english or french cards, I never know, it traced different lengths for each suit.

    Thanks again though, it is great help. I will keep practicing.

    By the way, can you please tell me how this statement works?

    _global.whoseTurn = (res.t == 1) ? 2 : 1

    setTurn()

    is there any default value in this statement? I know it's ternary but I now need to apply some method to my card game instead of this and I'm confused with it. I guess it means if player (from the obj) is 1, change turn and viceversa, correct? Is anything similar possible for 4 people or will I need to write several conditional statements?
    Last edited by capdetrons; 03-20-2014 at 02:29 PM.

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    I told you arrays are hard and this cards problem is hard. I made a complete fla because a card game is a good way to learn arrays. I'm going to start making example fla files that I can post.

    I went through your code and got it working i think. My computer has the spanish cards ghost so i can't tell.

    There's some comments in there.


    Code:
    private var aSuits: Array = ["oros", "copes", "espases", "bastos"];
    		private var aValues: Array = ["Manilla", "As", "Rei", "Cavall", "Sota", "8", "7", "6", "5", "4", "3", "2"];
    		private var aTemp: Array = new Array();
    		private var aDeck: Array = new Array();
    		private var aHands: Array = new Array();
    		private var nHandSize: Number = 5;
    		private var nNumberOfHands: Number = 4;
    
    
    
    		createDeck();
    
    
    		function createDeck(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < 4; i++) {
    				for (j = 0; j < 12; j++) {
    					//var oCard:Object = {sCardSuit:[i], sCardNumber:[j]};
    					var oCard: Object = new Object();
    					oCard.nCardSuit = i;
    					oCard.nCardValue = j;
    					oCard.nCardNumber = aTemp.length; //  the card number is a unique number for each card, not the suit or value
    					aTemp.push(oCard);
    				}
    			}
    			randomizeDeck();
    		}
    
    		function randomizeDeck(): void {
    			var i: int = 0;
    			var j: int = 0;
    			while (aTemp.length > 0) {
    				var r: Number = Math.floor(Math.random() * aTemp.length);
    				aDeck.push(aTemp[r]);
    				aTemp.splice(r, 1);
    			}
    			createHands();
    		}
    
    		function createHands(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < nNumberOfHands; i++) {
    				aHands[i] = new Array(); //  create a new hand.  an array of oCard
    				for (j = 0; j < nHandSize; j++) {
    					aHands[i].push(aDeck.pop()); //  push the last oCard in aDeck into the new hand					
    				}
    			}
    			showHands();
    		}
    
    		function showHands(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < nNumberOfHands; i++) {
    				trace("Player " + i + ": ");
    				aHands[i].sortOn(["nCardValue"], [Array.NUMERIC]); //  sort on card value
    				for (j = 0; j < nHandSize; j++) {
    					trace("Card " + j + ": " + aValues[aHands[i][j].nCardValue] + " of " + aSuits[aHands[i][j].nCardSuit]);
    				}
    				trace("----------");
    			}
    		}
    That statement is a "shorthand" "if" statement: result = condition ? (if true) : (else)

  7. #7
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    Thanks a lot, Moot,

    I appreciate the help. I just began a job as a driver and hardly have time to deal with this but I will look a it every so often. I can't get started with classes yet, I think. It confuses me too much, so I'll stay with the code in the clients' timelines for now, but about the statement; will there be a simple way like it for 4 players? I'm trying to substitute it with if and else statements and I've only got it to work for the dealing...

  8. #8
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    950
    As moot says, this: result = condition ? (if true) : (else)
    It's called the Ternary Statement. Is a short and quick way of doing this: if(condition == true){ //do this } else { //do this other thing }
    Hi Object, Listen, i wish to return you something, but only if what you are saying is true, else, forget it, ROLL_OVER the floor, i will check for someone else at the stage, while eating a no.visible cookie.

  9. #9
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    The code I posted is your AS2 code edited. I can't use AS2 so just copy and paste it in and change whatever gets an error - i cant see the As2 errors.

    Code:
    var aSuits: Array = ["oros", "copes", "espases", "bastos"];
    var aValues: Array = ["Manilla", "As", "Rei", "Cavall", "Sota", "8", "7", "6", "5", "4", "3", "2"];
    var aTemp: Array = new Array();
     var aDeck: Array = new Array();
    var aHands: Array = new Array();
    var nHandSize: Number = 5;
    var nNumberOfHands: Number = 4;
    
    
    
    		createDeck();
    
    
    		function createDeck(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < 4; i++) {
    				for (j = 0; j < 12; j++) {
    					//var oCard:Object = {sCardSuit:[i], sCardNumber:[j]};
    					var oCard: Object = new Object();
    					oCard.nCardSuit = i;
    					oCard.nCardValue = j;
    					oCard.nCardNumber = aTemp.length; //  the card number is a unique number for each card, not the suit or value
    					aTemp.push(oCard);
    				}
    			}
    			randomizeDeck();
    		}
    
    		function randomizeDeck(): void {
    			var i: int = 0;
    			var j: int = 0;
    			while (aTemp.length > 0) {
    				var r: Number = Math.floor(Math.random() * aTemp.length);
    				aDeck.push(aTemp[r]);
    				aTemp.splice(r, 1);
    			}
    			createHands();
    		}
    
    		function createHands(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < nNumberOfHands; i++) {
    				aHands[i] = new Array(); //  create a new hand.  an array of oCard
    				for (j = 0; j < nHandSize; j++) {
    					aHands[i].push(aDeck.pop()); //  push the last oCard in aDeck into the new hand					
    				}
    			}
    			showHands();
    		}
    
    		function showHands(): void {
    			var i: int = 0;
    			var j: int = 0;
    			for (i = 0; i < nNumberOfHands; i++) {
    				trace("Player " + i + ": ");
    				aHands[i].sortOn(["nCardValue"], [Array.NUMERIC]); //  sort on card value
    				for (j = 0; j < nHandSize; j++) {
    					trace("Card " + j + ": " + aValues[aHands[i][j].nCardValue] + " of " + aSuits[aHands[i][j].nCardSuit]);
    				}
    				trace("----------");
    			}
    		}
    I took out the "private"s. I cant see what else causes an error. Say what the error is.

  10. #10
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    No, none, there was none. I like it. I will probably apply it. Thanks

    it's the ternary statement which I'd like to be able to use for four players though.

    would a switch case be better than ifs and elses ?

    Code:
    var t:String = resObj.t
    
    switch (t) {
    case "1" :
    	player 1's turn;
    	break;
    case "2" :
    	player 2's turn;
    	break;
    case "3" :
    	player 3's turn;
    	break;
    case "4" :
    	player 4's turn;
    	break;
    }

  11. #11
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    I don't get what you're asking?

    The ternary operator is only for short "if" statements.

    Nothing but strings should be stored in strings. The turn is a number. Try to use dynamic referencing in place of checking to see what the turn number is.

    aHands[turnNumber] is the hand of the current turn.

  12. #12
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    You're right! I'm sorry.

    I'm just very confused with the problem I'm having now on the server side. I'm basing myself on one example smartfox server has on the web, but it's only for two players so I'm having extra trouble understanding this.

    I am trying to pass the turn to the client after positioning the card plus passing its value but I can't solve it and it seems weird because the way I created a function to pass the turn on starting the hand works.

    Code:
    function handleStart(prms, u)
    {
    	var obj = {}
    	obj._cmd = "startHand"
    	obj.tr = prms.tr
    	obj.t = whoseTurn + 1
    				
    	_server.sendResponse(obj, currentRoomId, null, users)
    }
    
    function handleMove(prms, u)
    {
    	var px = prms.x
    	var py = prms.y
    	var value = prms.value
    				
    	var obj = {}
    	obj._cmd = "move"
    	obj.x = px
    	obj.y = py
    	obj.t = whoseTurn + 1
    	obj.value = value
    				
    	_server.sendResponse(obj, currentRoomId, null, users)
    }
    The first function would set the trump and pass the turn and the second one stays in player 1 when I'm back on the client. Could the value parameter in the obj cause any conflict perhaps? I really don't know if it's a reserved word which I'm not using properly.

  13. #13
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Don't bother checking, just change it. If you're going to use regular words for variable names, avoid anything that could be a reserved word anywhere.

    What is the error exactly? obj.t is incorrect?

  14. #14
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    Yes, it doesn't work. And it doesn't seem to get passed to the client where I have a trace statement for it in the receiving function. The previous one does work though, that's why I'm wondering what may go on. I don't think there may be a limit for the parameteres passed in the objects.

  15. #15
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    lol "doesn't work" isn't a good description.

    There's no response? There's a response and there's no obj.t? There's a response but obj.t is incorrect?

  16. #16
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    950
    I have not followed this thread so i don't know much about it, but try getting obj object reference out of handleStart function, so you have
    reference to it everywhere.

    var obj:Object = new Object();
    function handleStart(prms, u)
    {
    obj = {};
    Hope that helps something.
    Hi Object, Listen, i wish to return you something, but only if what you are saying is true, else, forget it, ROLL_OVER the floor, i will check for someone else at the stage, while eating a no.visible cookie.

  17. #17
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    Day,

    sorry about that. Doesn't function then, no funciona, kaput, nicht gut, ne, malo

    there is a response but obj.t won't even trace out whilst the rest of the params do get back. Cards are placed in their new positions and their value traces out from the client side.

    Still haven't tried out what angel suggests...

    in the function before this one, I coded the turn exactly the same way whoseTurn + 1 before passing it and it is received.

  18. #18
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Yeah I can't help with the backend stuff unless I can see all the code and know what the output is.

  19. #19
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    309
    Hi, Moot, good day,

    I'm attaching my extension on the server side plus the code in the client side. I've just noticed my extension handler receives the turn in the client side so the problem must be in the setTurn function in the client even though I still wonder why it won't react or respond (not to mention work ) like the handleStart function which allows the turn change.

  20. #20
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    Sorry, but I can't help with this.

    I'd need the _server.sendResponse() code (this function and any code it uses), the backend code or api, and I'd need to know what the messages or output is.

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

Poll by Flashkit.com