Difference between revisions of "Mutation Operators"

From GiderosMobile
 
(2 intermediate revisions by one other user not shown)
Line 17: Line 17:
 
=== Examples ===
 
=== Examples ===
 
'''Simple larger example'''
 
'''Simple larger example'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
score+=1 -- faster and less typing than score=score+1
 
score+=1 -- faster and less typing than score=score+1
</source>
+
</syntaxhighlight>
  
 
'''"getDistance" function''' ('''see also the new <big>[[Math.distance]]</big> function''')
 
'''"getDistance" function''' ('''see also the new <big>[[Math.distance]]</big> function''')
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
--Non-optimized variant (5x slower):
 
--Non-optimized variant (5x slower):
 
function getDistance( x1, y1, x2, y2 )
 
function getDistance( x1, y1, x2, y2 )
Line 32: Line 32:
 
return ((x2 - x1)^2 + (y2 - y1)^2)^0.5
 
return ((x2 - x1)^2 + (y2 - y1)^2)^0.5
 
end
 
end
</source>
+
</syntaxhighlight>
 
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
=== Methods ===
 
| style="width: 50%; vertical-align:top;"|
 
=== Events ===
 
=== Constants ===
 
|}
 
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 03:00, 1 December 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
Available since: Gideros 2017.11

Description

Operators to mutate a value.

  • a += b = add 'b' to 'a'
  • a -= b = subtract 'b from 'a'
  • a *= b = multiply 'a' by 'b', result in 'a'
  • a /= b = divide 'a' by 'b', result in 'a'
  • a %= b = the result of 'a%b' is placed in 'a'
  • a ^= b = the result of 'a^b' is placed in 'a' (^ is faster than a=math.pow(a,b))
  • a ^= 0.5 = the result is faster than a=math.sqrt(a)

Examples

Simple larger example

score+=1 -- faster and less typing than score=score+1

"getDistance" function (see also the new Math.distance function)

--Non-optimized variant (5x slower):
function getDistance( x1, y1, x2, y2 )
	return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
end

--Optimized variant (5x faster):
function getDistance2(x1, y1, x2, y2)
	return ((x2 - x1)^2 + (y2 - y1)^2)^0.5
end