Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Zack

Thread: Migrate AS3 to Java

  1. #1
    M4tchB0X3r is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Migrate AS3 to Java

    Hi Forum,

    im quite new to Java, but i've been programming AS3 for quite a while now and it's time to dig more into java. So i'll be around more often in future
    Not much difference, i know, but i'm haveing problems with the datatypes.
    I made a flash game that expects an Array of Integers, which atm i am generating inside flash, for various reasons these need to come from the server. Ive got a java servlet and sending the array is not the problem, but when i try to convert my number generator from as3 to java i cant get seem to get the right datatypes.

    this is my generator in as3.

    Java Code:
    function generateRandomNumbers()
    		{
    			var randomNumbers = new Array;
    			var lastRanNum:uint;
    			var ranNumber:uint;
    			var superProbability = 0.5;
    			var jokerProbability = 0.2;
    			var superJokerProbability = 0.2;
    			var ranSuperNumber:uint;
    			var fieldLength:uint = 85;
    
    			for (var i:uint=0; i<(fieldLength); i++)
    			{
    				var ranSuper = Math.random();
    				var ranJoker = Math.random();
    				var ranSuperJoker = Math.random();
    
    				if (ranSuper > superProbability)
    				{
    
    					if (ranJoker < jokerProbability)
    					{
    						randomNumbers.push(0);
    					}
    					else
    					{
    						ranNumber = Math.floor(Math.random() * 9) + 1;
    						while (ranNumber == lastRanNum || ranNumber == lastRanNum + 10)
    						{
    							ranNumber = Math.floor(Math.random() * 9) + 1;
    						}
    
    						lastRanNum = ranNumber;
    						randomNumbers.push(ranNumber);
    					}
    				}
    				else
    				{
    					if (ranSuperJoker < superJokerProbability)
    					{
    						randomNumbers.push(10);
    					}
    					else
    					{
    						ranSuperNumber = Math.floor(Math.random() * 9) + 10;
    						while (ranSuperNumber == lastRanNum || ranSuperNumber == lastRanNum - 10)
    						{
    							ranSuperNumber = Math.floor(Math.random() * 9) + 10;
    						}
    						lastRanNum = ranSuperNumber;
    						randomNumbers.push(ranSuperNumber);
    					}
    
    				}
    
    			}
    
    			//MonsterDebugger.trace(this,randomNumbers);
    		}
    var randomNumbers = new Array;
    in as3 need to become
    randomList = new ArrayList<Integer>();

    How would i write this in java? Help would be somuch appreciated , i'm sure it will help me understanding the datatype situation alot better.
    THX
    ps:i can help out in any as3/flash related issues for the future

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    How would i write this in java?
    Java Code:
    ArrayList<Integer> randomList = new ArrayList<Integer>();

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    @Fubarable: I think he means Java; he refers to a servlet, ArrayList, and Integer types (none of which are present in JavaScript).
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    M4tchB0X3r is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Yes i mean Java! i'm fetching vars from a DB with java persistence, and additionally i want to create that array and send it to flash along with the rest.
    i'm haveing troubles with this for example
    Java Code:
    ranNumber = Math.floor(Math.random() * 9) + 1;
    java semms to expect ranNumber to be a Double, but to add it to the ArrayList it needs to be an int
    whe i try
    Java Code:
    ranNumber.intValue()
    it cant resolve method
    Last edited by M4tchB0X3r; 07-02-2011 at 10:04 PM. Reason: typo's XD

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Java is case sensitive. Double is a class and double is a primitive.
    The Math.floor() method returns a double. You can cast it to an int by adding (int) before the Math....

    to add it to the ArrayList it needs to be an int
    Another item. You can only add objects to an ArrayList. So you would be adding Integer class objects.
    The compiler with automatically convert (autobox) an int to an Integer if you use the add() method.

  7. #7
    M4tchB0X3r is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    THX!!!

    Java Code:
    ranNumber = [B](int)[/B]Math.floor(Math.random() * 9) + 1;
    thats what i was looking for ....

Similar Threads

  1. Migrate from PHP to J2EE
    By venky in forum Advanced Java
    Replies: 0
    Last Post: 06-01-2010, 09:24 PM
  2. migrate JSP website
    By valhensing in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-14-2009, 03:34 PM
  3. This project needs to migrate WPT metadata
    By Arthur Gardner in forum Eclipse
    Replies: 5
    Last Post: 12-11-2009, 10:49 PM
  4. migrate to Netbeans
    By Jack in forum NetBeans
    Replies: 3
    Last Post: 07-02-2007, 01:26 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
  •