Advertisement
angelhdz12

Shared Object and Literal Syntaxes

Aug 3rd, 2017
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {name: "Angel"} is the LITERAL representation of a generic object or JSON.
  2.  
  3. All classes inherit from Object.
  4.  
  5. You really need to read more about Object Oriented Programming.
  6.  
  7. So, the SharedObject.data property, is a getter, that first reads the .so file, parse the data, and returns a generic object with all the key/values pairs. You just need to iterate through the elements of such object, or accessing the values directly by their keys:
  8.  
  9. //Object
  10. var localData:Object = {};
  11. localData = so.data;
  12.  
  13. for(var i:String in localData)
  14. {
  15. trace( "Key:" + i + " Value: " + localData[ i ] ) ;
  16. }
  17.  
  18. Or:
  19.  
  20. trace( localData.levelNumber );
  21.  
  22. You will get what you saved. If you saved an array, you will get an Array. If you saved a boolean, you will get a Boolean.
  23.  
  24. The thing is, if you use an if statement like this:
  25.  
  26. if( localData.score )
  27.  
  28. It will return true, regardless of if 'score' is true or false, because that is checking for null, not for true/false.
  29.  
  30. Example:
  31.  
  32. var localData:Object = { name: "Angel" };
  33.  
  34. if( localData.score )
  35. {
  36. //The body of the if statement will not execute, because score is null
  37. }
  38.  
  39. Now, let's add a Boolean:
  40.  
  41. var localData:Object = { name: "Angel", "hasLives": false };
  42.  
  43. if( localData.hasLives )
  44. {
  45. // The body of the if statement will execute, because, although hasLives is false, it will return true because hasLive is not null
  46. }
  47.  
  48. So in your case, you need to do both checks:
  49.  
  50. if( localData.hasLives && localData.hasLives == true)
  51. {
  52. //Now, this won't be executed because one of the conditions
  53. didn't met. That means, hasLives is not null, but it is false.
  54. }
  55.  
  56.  
  57. An object returns true, not only if it's a boolean set to true, but also if it's not null or it's not undefined.
  58.  
  59. An object returns false if it's null and it's undefined, so when we do this:
  60.  
  61. if( object )
  62.  
  63. We are just checking if it's not null and if it's not undefined.
  64.  
  65. So to check if it's true or false (boolean), we explicitly check it:
  66.  
  67. if( object == true )
  68. {
  69.  
  70. }
  71.  
  72. Let's take this example:
  73.  
  74. var localData:Object = {"musicIsOn": null};
  75.  
  76. if( localData.musicIsOn )
  77. {
  78. //This will NOT be executed, because musicIsOn is defined as null, that means it's null, although the key 'musicIsOn' exists in the object.
  79. }
  80.  
  81. Another example:
  82.  
  83. trace( localData.musicIsOn == null ); //true
  84. trace( localData.musicIsOn == undefined ); //true
  85.  
  86. It returns true in both cases because musicIsOn is null, and as it is null, it is also undefined.
  87.  
  88.  
  89.  
  90.  
  91. Other literal representations:
  92.  
  93. Array:
  94.  
  95. []
  96.  
  97. Object:
  98.  
  99. {}
  100.  
  101. Vector:
  102.  
  103. <TypeHere>[]
  104.  
  105. String:
  106.  
  107. ""
  108.  
  109. Number:
  110.  
  111. 123.99
  112.  
  113. int:
  114.  
  115. 55
  116.  
  117. Function:
  118.  
  119. function():void{}
  120.  
  121. Boolean:
  122.  
  123. true / false
  124.  
  125. Expressions:
  126.  
  127. ()
  128.  
  129. XML:
  130.  
  131. <>
  132.  
  133.  
  134.  
  135.  
  136. uint:
  137.  
  138. 0xff0000
  139.  
  140. Not a Number:
  141.  
  142. NaN
  143.  
  144. Regular Expressions:
  145.  
  146. / /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement