How do you make something spawn when you collide with a certain box?
This is the code I had:
box=game.spawnXY(“box”, 19, 50)
box1=game.spawnXY(“box”, 30, 50)
box2=game.spawnXY(“box”, 40, 29)
box3=game.spawnXY(“box”, 49, 29)
box4=game.spawnXY(“box”, 75, 20)
def onCollide(event):
# The event owner who has collided with something.
unit = event.target
# The object the unit collided with.
collidedObject = event.other
# If it’s a fence.
if collidedObject==box2:
x=collidedObject.pos.x
y=collidedObject.pos.y
game.spawnXY(“flower”, x, y)
collidedObject.destroy()
player.on(“collide”, onCollide)
Hi @Mrs_Lee! (We share the same last name ) Welcome to the CodeCombat Discourse! This is a safe place to chat and ask for level help. Before you begin your journey, could you please go through our rules that make this discourse an awesome place? It helps us all We hope you enjoy your stay!!
Now, with that out of the way, could you please specify what level this is, and format your code properly? (If you don’t know how to, click here)
This is from Hour of Code Intermediate Game Dev from level Code, Play, Share :
box=game.spawnXY(“box”, 19, 50)
box1=game.spawnXY(“box”, 30, 50)
box2=game.spawnXY(“box”, 40, 29)
box3=game.spawnXY(“box”, 49, 29)
box4=game.spawnXY(“box”, 75, 20)
def onCollide(event):
# The event owner who has collided with something.
unit = event.target
# The object the unit collided with.
collidedObject = event.other
# If it’s a fence.
if collidedObject==box2:
x=collidedObject.pos.x
y=collidedObject.pos.y
game.spawnXY(“flower”, x, y)
collidedObject.destroy()
player.on(“collide”, onCollide)
I believe your problem may be an indentation error. If you indent the collide function as shown below, that should fix the error.
def onCollide(event):
# The event owner who has collided with something.
unit = event.target
# The object the unit collided with.
collidedObject = event.other
# If it’s a fence.
if collidedObject==box2:
x=collidedObject.pos.x
y=collidedObject.pos.y
game.spawnXY(“flower”, x, y)
collidedObject.destroy()