how do I attach a smiley face to a bullet?(resolved)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
worldaway
Prole
Posts: 44
Joined: Thu Sep 08, 2011 2:22 am

how do I attach a smiley face to a bullet?(resolved)

Post by worldaway »

I have taken the physics tutorial, and made it so that the ball is a smiley face (smileyface.png), and it can shoot bullets upwards. What I wan't to do is attach a smaller picture of a smiley face to the bullet. I have the smaller picture (shotb.png), but can't figure out how to attach it.

code:

Code: Select all

function love.load()
   world = love.physics.newWorld(0, 0, 650, 650) --create a world for the bodies to exist in with width and height of 650
   world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
   world:setMeter(64) --the height of a meter in this world will be 64px

   objects = {} -- table to hold all our physical objects

   --let's create the ground
   objects.ground = {}
   --we need to give the ground a mass of zero so that the ground wont move
   objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
   objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50

   --let's create a ball
   objects.ball = {}
   objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
   objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20
   objects.ball.shots = {} -- holds shots

   --initial graphics setup
   love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
   love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
   
   HappyFace = love.graphics.newImage("HappyFace.png")
   shotb = love.graphics.newImage("shotb.png")
end


function love.update(dt)
   world:update(dt) --this puts the world into motion

   --here we are going to create some keyboard events
   if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
      objects.ball.body:applyForce(400, 0)
   elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
      objects.ball.body:applyForce(-400, 0)
   elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
      objects.ball.body:setY(650/2)
   end
   -- update the shots
   for i,v in ipairs(objects.ball.shots) do
      -- move them up up up
      v.y = v.y - dt * 500
	  
   end
end


function love.draw()
   love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
   love.graphics.polygon("fill", objects.ground.shape:getPoints()) -- draw a "filled in" polygon using the ground's coordinates

   love.graphics.setColor(255,255,0) --set the drawing color to yellow for the ball
   local b = objects.ball.body
   love.graphics.draw(HappyFace, b:getX(), b:getY(), b:getAngle(), 1, 1, HappyFace:getWidth()/2, HappyFace:getHeight()/2)

   -- let's draw our balls shots
   love.graphics.setColor(255,255,255,255)
   for i,v in ipairs(objects.ball.shots) do
      local s = objects.ball.shots
      love.graphics.rectangle("fill", s:getX(), s:getY(), 2, 5)
   end
end


function love.keyreleased(key)
   if key == "w" then
      shoot()
   elseif key == "d" then
	  shoot()
   elseif key == "a" then
      shoot()   
   end
end

function shoot()
   local shot = {}
   shot.x, shot.y = objects.ball.body:getPosition()
   table.insert(objects.ball.shots, shot)
end
Please help,
Thanks.
Last edited by worldaway on Thu Oct 13, 2011 12:13 am, edited 2 times in total.
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: how do I attach a smiley face to a bullet?

Post by felix24 »

as far as I can see all you need to do is:
in love.draw(), instead of

Code: Select all

love.graphics.rectangle("fill", s:getX(), s:getY(), 2, 5)
have

Code: Select all

love.graphics.draw(shotb, s:getX(), s:getY(), 0, 1, 1, shotb:getWidth()/2, shotb:getHeight()/2)
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: how do I attach a smiley face to a bullet?

Post by kraftman »

just a note that there are code tags on the forum that you can use to preserve the formatting of your code, which makes it easier for us to read and easier to help :)
worldaway
Prole
Posts: 44
Joined: Thu Sep 08, 2011 2:22 am

Re: how do I attach a smiley face to a bullet?

Post by worldaway »

felix24 wrote:as far as I can see all you need to do is:
in love.draw(), instead of

Code: Select all

love.graphics.rectangle("fill", s:getX(), s:getY(), 2, 5)
have

Code: Select all

love.graphics.draw(shotb, s:getX(), s:getY(), 0, 1, 1, shotb:getWidth()/2, shotb:getHeight()/2)
I tried that and it gave me the same error "attempt to call method getX (a nill value)"
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: how do I attach a smiley face to a bullet?

Post by felix24 »

ok yeah sorry. that's cause getX() doesn't apply to your shots. you should just be able to use .x and .y.

try this:

Code: Select all

love.graphics.draw(shotb, v.x, v.y, 0, 1, 1, shotb:getWidth()/2, shotb:getHeight()/2)
also, you don't need this line

Code: Select all

local s = objects.ball.shots
hope this helps...
worldaway
Prole
Posts: 44
Joined: Thu Sep 08, 2011 2:22 am

Re: how do I attach a smiley face to a bullet?

Post by worldaway »

felix24 wrote:ok yeah sorry. that's cause getX() doesn't apply to your shots. you should just be able to use .x and .y.

try this:

Code: Select all

love.graphics.draw(shotb, v.x, v.y, 0, 1, 1, shotb:getWidth()/2, shotb:getHeight()/2)
also, you don't need this line

Code: Select all

local s = objects.ball.shots
hope this helps...
Works! thank you very much.
Post Reply

Who is online

Users browsing this forum: No registered users and 235 guests