Topic was automatically imported from the old Question2Answer platform.
Asked By
Magiel
Hi,
I’m very new to the development scene
I’m trying to create a 2d fighting game where when you enter the “arena” scene different (random) backgrounds would appear.
I’ve created 8 different backgrounds that should be randomized.
I have no idea what I’m doing and any help would be appreciated.
First of all you need to set up an Area2D node in order to detect collisions. You select the Area2d node, you assign a shape to it (say, a cube) and you hook up the area_entered() signal (from the Node panel on the right) to your Player node. When the object in question enters that area, the background should change.
Now, I presume you already know how to place a background (you can place a large Sprite behind the other objects). The way you can change that background randomly is this:
var rnum # random number variable
var bg_array = [] # array holding the backgrounds
var bg_texture1 = preload("res://<path>/<to>/<texture>/texture1.png")
var bg_texture2 = preload("res://<path>/<to>/<texture>/texture2.png")
...
var bg_texture8 = preload("res://<path>/<to>/<texture>/texture8.png")
func _ready():
bg_array = [bg_texture1, bg_texture2..., bg_texture8]
func _on_Area2D_area_entered(area): # area2d node entered
randomize() # call that before every 'truly' random number generation
rnum = randi()%9 #generate a random integer from 0 to 8
Background_Sprite.set_texture(bg_array[rnum]) # set the texture of your Sprite to a random background from the previously populated bg_array
Does this help you? If it does, please mark this answer as best, so that others can find it .