Page 1 of 1

Transparent areas of player image fill with foreground color

Posted: Thu Aug 08, 2013 6:37 am
by AdventureFan
I'm teaching myself Love2D, currently I'm creating a space invaders clone (followed a tutorial then tried to program it on my own).

I created a player by using the following code.

- in love.load:

Code: Select all

player.graphic = love.graphics.newImage('player_small.png')
- in love.draw:

Code: Select all

love.graphics.setBackgroundColor(0,0,0)
love.graphics.setColor(255, 0, 0)
   
   -- draw the player   
   love.graphics.draw(player.graphic, player.x, player.y)
However, the transparent areas of my image take on the red foreground color. I the background just to be black or transparent so it shows the background color.

Re: Transparent areas of player image fill with foreground c

Posted: Thu Aug 08, 2013 6:47 am
by raidho36
Can you post a screenshot or preferably minimum file reproducing the problem?

Re: Transparent areas of player image fill with foreground c

Posted: Thu Aug 08, 2013 7:00 am
by AdventureFan
Here you go. As you can see, there is a square block of "red" around my player.

This is the full game code:

Code: Select all

function love.keypressed(key)
   if key == " " then
       -- shoot a bullet (by adding it to the bullets table and passing a .y value)
      bullets[#bullets+1] = {
      y = 495,
      speed = 300,
      width = 10,
      height = 5,
      x = (player.x + player.width/2) - 5
      }
   end
end   

function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)

  local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
  return ax1 <  bx2 and ax2 < bx1 and ay1 < by2 and ay2 < by1
end

function love.load()

   -- setup the player
   player = {}
   player.x = 300
   player.y = 500
   player.speed = 300
   player.graphic = love.graphics.newImage('player_small.png')
   player.width = player.graphic:getWidth()
   player.height = player.graphic:getHeight()
 
   
   -- setup the bullets
   bullets = {}

   -- setup the enemies
   enemies = {}

   for i=0,7 do
      enemy = {}
      enemy.width = 40
      enemy.height = 20
      enemy.speed = 20
      enemy.x = i * (enemy.width+60) + 100
      enemy.y = enemy.height + 100
      table.insert(enemies, enemy)
   end
   
   -- when a key is pressed, call the function love.keypressed
  love.keypressed()
   
end

function love.update(dt)
   -- move our player
   if love.keyboard.isDown("right") then
      if player.x < (800-player.width) then
         player.x = player.x + player.speed *dt
      elseif player.x < (800-player.width) then
         player.x = (800-player.width)
      end
   end
   
   if love.keyboard.isDown("left") then
      if player.x > 0 then
         player.x = player.x - player.speed *dt   
      elseif player.x < 0 then
         player.x = 0
      end   
   end

   -- update the position of the enemies in the table 'enemies'
   for i,v in ipairs(enemies) do
      v.y = v.y + v.speed * dt     
   end
   
   -- update the position of the bullets in the table 'bullets'
   for i,v in ipairs(bullets) do
      
      -- move the bullets upward
      v.y = v.y - v.speed * dt

      
      -- Check for collision between bullets and enemies. If they collide, remove both bullets and emies
      for ii,vv in ipairs(enemies) do
         if v.y < vv.y and (v.x > vv.x and v.x < vv.x+vv.width) then
            table.remove(enemies, ii)
            table.remove(bullets, i)
         end
      end

      -- remove shots that are out of range
       if v.y < 120 then
         table.remove(bullets, i)
      end

   end

end

function love.draw()
   
   love.graphics.setBackgroundColor(255,255,255)
   love.graphics.setColor(255, 0, 0)
   
   -- draw the player   
   love.graphics.draw(player.graphic, player.x, player.y)

   -- draw the bullets
   love.graphics.setColor(255, 255, 255)
   for i, v in ipairs(bullets) do
      love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
   end

   -- draw the enemies
   love.graphics.setColor(0, 255, 255)
   for i, v in ipairs(enemies) do
      love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
   end

end

Re: Transparent areas of player image fill with foreground c

Posted: Thu Aug 08, 2013 8:01 am
by Davidobot
I'm pretty sure it is just that your image background isn't 100% transperant, but more like 10 Alpha or something.

Re: Transparent areas of player image fill with foreground c

Posted: Thu Aug 08, 2013 8:16 am
by AdventureFan
Davidobot wrote:I'm pretty sure it is just that your image background isn't 100% transperant, but more like 10 Alpha or something.
Yeah, you are right. I actually forgot to add tranparency to the player image. Changed that, and it works fine now.

I thought it was the code, but apparently it was the image...

Re: Transparent areas of player image fill with foreground c

Posted: Thu Aug 08, 2013 8:46 am
by jjmafiae
also please think before posting cause this is the wrong forum (post support & development when its things like this).