Physic + Sprites
I need a code for this because I can’t change mine when it is hit
You might want to go into a bit of detail with your question, We can’t really understand what you are asking. I will try to answer what I think is your question.
I am complete beginner and learning through ignatz guides(thanks btw, really helpful), so i cant go into technical specifics. If you want to change your sprite then i am pretty sure that you will have to use an if statement, like so: If (name of a function that means object is hit) then (another function that changes the animation). Something along that line, i cant go into anymore specifics myself, if you need more indepth insight then look into some guides. Like i said, i am a beginner myself so i cant go into technicalities. If you need to be pointed toward some guides just ask.
function setup()
c1=physics.body(CIRCLE,20)
c1.x=WIDTH/2
c1.y=HEIGHT/4
c1.type=STATIC
c2=physics.body(CIRCLE,20)
c2.x=WIDTH/2
c2.y=HEIGHT/1.5
c2.restitution=1
end
function draw()
background(40, 40, 50)
if change then
sprite("Planet Cute:Character Boy",c1.x,c1.y)
sprite("Planet Cute:Character Pink Girl",c2.x,c2.y)
else
sprite("Planet Cute:Character Pink Girl",c1.x,c1.y)
sprite("Planet Cute:Character Boy",c2.x,c2.y)
end
end
function collide(c)
if c.state==BEGAN then
change=not change
end
end
It made 2 massive ones of the second sprites, Do I have to redo it so I is all there
DTK I was asking for co to change my sprite if It is hit,
We are making a Angry birds type game
Thanks guys
@RobinBundles Here’s something else. Without seeing your code, it’s hard to know exactly what you want or how to fix it.
supportedOrientations(LANDSCAPE_ANY)
function setup()
hit = false
c1=physics.body(CIRCLE,20)
c1.x=WIDTH/2
c1.y=HEIGHT/2
c1.type=STATIC
c2=physics.body(CIRCLE,20)
c2.x=10
c2.y=10
c2.restitution=1
c2.gravityScale=0
c2.linearVelocity=vec2(200,225)
end
function draw()
background(40, 40, 50)
fill(255)
ellipse(c2.x,c2.y,40)
if hit then
sprite("Planet Cute:Character Pink Girl",c1.x,c1.y)
else
sprite("Planet Cute:Character Boy",c1.x,c1.y)
end
end
function collide(c)
if c.state==BEGAN then
hit = true
end
end
I made my own example, that uses its own class to handle everything, to make a heart that falls on a line, and darkens for 0.75 seconds when it is hit. It’s also well commented and explains pretty much everything.
-- Collision Animation
-- Use this function to perform your initial setup
function setup()
floor = physics.body(EDGE, vec2(0, 50), vec2(WIDTH, 0)) -- Create the floor
c = Char(WIDTH / 4, HEIGHT / 2) -- Create the character
end
-- This function gets called once every frame
function draw()
-- This sets a light background color
background(255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
stroke(0) -- Dark stroke
line(0, 50, WIDTH, 0) -- Draw the floor
c:draw() -- Let the character handle its drawing
end
function collide(contact)
c:collide(contact) -- Let the character handle its own collisions
end
Char = class() -- Create the character's class
function Char:init(x, y)
self.obj = physics.body(CIRCLE, 50) -- Create and configure a physics body
self.obj.x = x
self.obj.y = y
self.obj.restitution = 0.5
self.hurtTimer = 0 -- Hurt timer
end
function Char:draw()
self.hurtTimer = math.max(0, self.hurtTimer - DeltaTime) -- Lowers the hurt timer, math.max ensures it's greater than or equal to 0, and self.hurtTime - DeltaTime means that no matter how long it took to draw the frame, it always lowers it the samd amount.
pushMatrix() -- Push the matrix
translate(self.obj.x, self.obj.y) -- Translate drawing to the physics body's position, so rotating doesn't glitch
rotate(self.obj.angle) -- Rotate to the body's angle
if self.hurtTimer > 0 then -- Is the character hurt?
sprite("Small World:Heart Dark", 0, 0, 100) -- Darken the heart
else -- Not hurt
sprite("Small World:Heart Flat", 0, 0, 100) -- Lighten the heart
end
popMatrix() -- Pop the matrix
end
function Char:collide(contact)
if contact.bodyA == self.obj or contact.bodyB == self.obj and contact.state == BEGAN then -- Is at least one of the affected bodies the character, and is the contact just starting?
self.hurtTimer = 0.75 -- Set the hurt timer to 0.75 seconds, configurable
end
end
That seems to have worked
My entire update function has gone Kaput when I put Sky’s code in. Why?
Try copying the “Char” class to a separate part of your code, maybe another tab, and try to use it then, like in the code. And don’t forget to edit function collide(contact) or it won’t change.
Also, can you try to better describe what happens when you used my code or share it, maybe?