Difference between revisions of "Sprite.overlaps"
Jump to navigation
Jump to search
(Add example) |
|||
Line 14: | Line 14: | ||
Use of this requires first defining the [[Sprite.localBounds|localBounds]] | Use of this requires first defining the [[Sprite.localBounds|localBounds]] | ||
of each sprite. | of each sprite. | ||
+ | |||
+ | === Example === | ||
+ | |||
+ | <ms>clear | ||
+ | |||
+ | // Load images | ||
+ | |||
+ | wumpusImg = file.loadImage("/sys/pics/Wumpus.png") | ||
+ | muffinImg = file.loadImage("/sys/pics/food/Muffin.png") | ||
+ | |||
+ | // Define sprites | ||
+ | |||
+ | wumpus = new Sprite | ||
+ | wumpus.image = wumpusImg | ||
+ | // Define localBounds for Wumpus! | ||
+ | wumpus.localBounds = new Bounds | ||
+ | wumpus.localBounds.width = wumpusImg.width | ||
+ | wumpus.localBounds.height = wumpusImg.height | ||
+ | |||
+ | muffin = new Sprite | ||
+ | muffin.image = muffinImg | ||
+ | // Define localBounds for muffin! | ||
+ | muffin.localBounds = new Bounds | ||
+ | muffin.localBounds.width = muffinImg.width | ||
+ | muffin.localBounds.height = muffinImg.height | ||
+ | |||
+ | // Push sprites to the display | ||
+ | display(4).sprites.push wumpus | ||
+ | display(4).sprites.push muffin | ||
+ | |||
+ | checkOverlap = function | ||
+ | text.clear | ||
+ | if wumpus.overlaps(muffin) then | ||
+ | print "Eating muffin!" | ||
+ | else | ||
+ | print "No overlap" | ||
+ | end if | ||
+ | end function | ||
+ | |||
+ | // Case 1: No overlap | ||
+ | |||
+ | wumpus.x = 300 | ||
+ | wumpus.y = 400 | ||
+ | |||
+ | muffin.x = 500 | ||
+ | muffin.y = 400 | ||
+ | |||
+ | checkOverlap | ||
+ | |||
+ | wait 2 | ||
+ | |||
+ | // Case 2: Overlap | ||
+ | |||
+ | wumpus.x += 150 | ||
+ | |||
+ | checkOverlap | ||
+ | </ms> | ||
[[Category:Mini Micro]] | [[Category:Mini Micro]] |
Latest revision as of 15:22, 4 May 2024
Sprite.overlaps
checks whether this sprite overlaps (touches) another one, by
comparing their bounds.
Arguments
Parameter Name | Type | Meaning |
---|---|---|
other | Sprite or Bounds | object to check for overlap with |
Usage Notes
Use of this requires first defining the localBounds of each sprite.
Example
clear
// Load images
wumpusImg = file.loadImage("/sys/pics/Wumpus.png")
muffinImg = file.loadImage("/sys/pics/food/Muffin.png")
// Define sprites
wumpus = new Sprite
wumpus.image = wumpusImg
// Define localBounds for Wumpus!
wumpus.localBounds = new Bounds
wumpus.localBounds.width = wumpusImg.width
wumpus.localBounds.height = wumpusImg.height
muffin = new Sprite
muffin.image = muffinImg
// Define localBounds for muffin!
muffin.localBounds = new Bounds
muffin.localBounds.width = muffinImg.width
muffin.localBounds.height = muffinImg.height
// Push sprites to the display
display(4).sprites.push wumpus
display(4).sprites.push muffin
checkOverlap = function
text.clear
if wumpus.overlaps(muffin) then
print "Eating muffin!"
else
print "No overlap"
end if
end function
// Case 1: No overlap
wumpus.x = 300
wumpus.y = 400
muffin.x = 500
muffin.y = 400
checkOverlap
wait 2
// Case 2: Overlap
wumpus.x += 150
checkOverlap