![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Robster |
![]() |
Old Version | Published before Godot 3 was released. |
Hi all,
I’m dynamically creating a sprite like so:
powerUpSprite = Sprite.new() #create a new sprite
add_child(powerUpSprite) #add it as a child
var powerUpImage #the texture of the powerup will live here
powerUpImage = load("res://textures/powerUpTestImage.png")
powerUpSprite.set_texture(powerUpImage)
powerUpSprite.set_pos(Vector2(100,100))
It works fine.
Now I want to create a CollisionShape2D and making it a child of that sprite.
It must basically:
- Follow the sprite (be a child)
- resize to be the same dimensions of the sprite
- have a collision rectangleShape2D added that matches the size of the CollisionShape2D
So I guess I’m ok to figure out all those points, but the initial part I’m stuck on is making the CollisionShape2D
a child of the sprite. Then making the rectangleShape2D
.
OK I’ve been playing further with this… Still stuck though
I have above but I’ve added the following:
var powerUpImage #the texture of the powerup will live here
powerUpSprite = Sprite.new() #create a new sprite
add_child(powerUpSprite) #add it as a child
powerUpImage = load("res://textures/"+powerUp+".png") #load the appropriate image file
powerUpSprite.set_texture(powerUpImage) #assign it to the sprite
#screen is 540 x 300. Fit the spawing in an appropriate random area within that
var posX = randi()%350+1
var posY = randi()%190+1
#now add some offset, so we don't appear behind bats or in walls etc
posX = posX + 100
posY = posY + 80
#now draw the powerup on screen
powerUpSprite.set_pos(Vector2(posX,posY))
#Create the collision shape
var shape = RectangleShape2D.new()
#resize the shape to be the same size as the sprite texture
shape.set_extents(Vector2(powerUpSprite.get_texture().get_width(),powerUpSprite.get_texture().get_height()))
#create the collission object
powerUpCollision = CollisionShape2D.new()
#attach the shape to the object
powerUpCollision.set_shape(shape)
#add it as a child
add_child(powerUpCollision)
So that works great, in that it creates the shape, it sizes it to match the sprite, but the problem is… it places it in the top left of the screen (at 0,0).
I can’t find anything in the docs that makes reference to moving that shape so it’s in the same location of the sprite.
Does anyone know how to do that? Thanks a tonne…
Robster | 2017-03-25 01:23
You shouldn’t be using CollisionShape2D in code as it’s only a helper class for the editor. You’re also missing a collision object of some kind (Area2D should work fine for a power-up). Do something like:
-Create an Area2D
-Create a RectangleShape2D like you’ve done above (if all your power-ups are the same shape you can re-use the RectangleShape2D)
-Use Area2D.add_shape to assign the shape to the Area2D
-Add your Sprite as a child of the Area2D
-Add the Area2D to the scene and set its position
mollusca | 2017-03-25 18:34
Thank you. I’ve tried this and am not having much luck. Have added my reply to the answer below to keep the thread organised.
Robster | 2017-03-31 05:32
@mollusca, I was having an issue with creating the CollisionShape2D
because I kept trying to mimick how it’s created in the Editor:
var shape = CircleShape2D.new()
var col = CollisionShape2D.new()
col.set_shape(col)
add_child(col)
shape.set_radius(wpn_sprite_tex.get_size().x)
But it would never work, so I changed it to:
var shape = CircleShape2D.new()
shape.set_radius(wpn_sprite_tex.get_size().x)
add_shape(shape)
And works great. Unfortunately it’s not visible if you turn on the “View Colliison Shapes” setting in the editor, but it works. Thanks for your help!!
wombatTurkey | 2020-01-16 01:00