Weird Alpha, Layers, Sound and Rotate image to mouse pos

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
User avatar
WolfosoBastardo
Prole
Posts: 4
Joined: Wed Jul 06, 2011 1:14 pm

Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by WolfosoBastardo »

Alright so(I started using LÖVE a few days ago, I've been scripting Lua on Garry's Mod for 2-3 years), there are three little problems I'm having:
1°Weird Alpha

As you can see
Image
Even though I'm setting color with this

Code: Select all

love.graphics.setColor( 226, 65, 65, 255 )
, alpha still doesn't appear to be 255. What am I doing wrong?

2° Layers.

Same image as above, you can see, the tank's barrel is above its base, which isn't suppose to happen. I tried to put the piece of code where I both load and draw the image above or below the base load / draw code, but nothing happened.

3° love.audio.play()

Whenever the tank shoots, it is supposed to play a sound, but it only works sometimes...

4° How do I rotate an image so that it always follows the mouse position? My brother suggested using sine and cosine but I couldn't seem to find the right operation on the wiki.

Thanks for anything helpful you are going to post!
Attachments
test.zip
Project
(1.43 MiB) Downloaded 164 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by Robin »

I'm not sure what exactly is going on here, but here are a few things I noticed:
  1. You uploaded a .zip with a .love in it. It is preferred to upload the .love directly to the forums.
  2. The color problems you have might be due to the colormode or the blendmode, I'm not sure. Try experimenting with them.
  3. Please don't put code that is not directly related to drawing things in love.draw. That's where love.update is for.
  4. To point something to something else, you need math.atan2. Specifically:

    Code: Select all

    rotation = math.atan2(drawy - mousey, mousex - drawx)
    Replace "rotation", "drawx", "drawy", "mousex" and "mousey" with the appropriate variables.
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by vrld »

  1. You set the blend mode to additive.
  2. Additive blending...
  3. It's a bug in love.audio. Until this get's fixed, use .ogg files. :roll:
  4. You can get the angle you need the image to rotate to point to the mouse using math.atan2:

    Code: Select all

    -- anchorx and anchor y are the points you want rotate the sprite around
    local mx, my = love.mouse.getPosition()
    local angle = math.atan2(mx - anchorx, my - anchory)
    
As for your code:
  • You do a lot of stuff in love.draw() that should be done in love.update(dt) instead. Things like fullscreen toggling and position updates have nothing to do with drawing.
  • Because of that, your code is depends on the framerate. That's not a good thing (use dt).
  • Events that should get triggered only once when a key (fullscreen toggling) or mouse button (shooting) is pressed are better moved to love.keypressed and love.mousepressed.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
WolfosoBastardo
Prole
Posts: 4
Joined: Wed Jul 06, 2011 1:14 pm

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by WolfosoBastardo »

Robin wrote:
  1. You uploaded a .zip with a .love in it. It is preferred to upload the .love directly to the forums.
  2. The color problems you have might be due to the colormode or the blendmode, I'm not sure. Try experimenting with them.
  3. Please don't put code that is not directly related to drawing things in love.draw. That's where love.update is for.
  4. To point something to something else, you need math.atan2. Specifically:

    Code: Select all

    rotation = math.atan2(drawy - mousey, mousex - drawx)
Alright I will do that next time.

Also thanks! I'll try that now.
vrld wrote:
  1. You set the blend mode to additive.
  2. Additive blending...
  3. It's a bug in love.audio. Until this get's fixed, use .ogg files. :roll:
  4. You can get the angle you need the image to rotate to point to the mouse using math.atan2:

    Code: Select all

    -- anchorx and anchor y are the points you want rotate the sprite around
    local mx, my = love.mouse.getPosition()
    local angle = math.atan2(mx - anchorx, my - anchory)
    
Alright I'll use .ogg file, thanks!
vrld wrote: As for your code:
  • You do a lot of stuff in love.draw() that should be done in love.update(dt) instead. Things like fullscreen toggling and position updates have nothing to do with drawing.
  • Because of that, your code is depends on the framerate. That's not a good thing (use dt).
  • Events that should get triggered only once when a key (fullscreen toggling) or mouse button (shooting) is pressed are better moved to love.keypressed and love.mousepressed.
Interesting, thanks, I'm going to do it now.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by Boolsheet »

vrld wrote:
  1. It's a bug in love.audio. Until this get's fixed, use .ogg files. :roll:
I don't think you can call it a bug.

An already playing source does not rewind itself if you pass it to love.audio.play(). You either have to stop the source first or create a few sources and play them in sequence so they overlap. There was talk about changing the behavior of play.
(The love.audio.rewind function seems broken. Is that just me? Edit: Ah, not broken. There is something strange going on though)

The wave decoder that LÖVE uses pads wav files to 5 seconds for some reason. This is probably not a bug, but a design decision by the developer of the library. So yeah, avoid wav for now. ;)
Shallow indentations.
User avatar
WolfosoBastardo
Prole
Posts: 4
Joined: Wed Jul 06, 2011 1:14 pm

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by WolfosoBastardo »

Alright just did everything suggested and everything works fine, except for love.keyboardpressed that of course only works when a key is pressed, not down; so in which function can I put the love.keyboard.isDown()?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by Robin »

WolfosoBastardo wrote:Alright just did everything suggested and everything works fine, except for love.keyboardpressed that of course only works when a key is pressed, not down; so in which function can I put the love.keyboard.isDown()?
That goes into love.update. But I don't think you'll want to use that for toggling fullscreen, because holding the "f" key gets really annoying then.
Help us help you: attach a .love.
User avatar
WolfosoBastardo
Prole
Posts: 4
Joined: Wed Jul 06, 2011 1:14 pm

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by WolfosoBastardo »

Robin wrote:
WolfosoBastardo wrote:Alright just did everything suggested and everything works fine, except for love.keypressed that of course only works when a key is pressed, not down; so in which function can I put the love.keyboard.isDown()?
That goes into love.update. But I don't think you'll want to use that for toggling fullscreen, because holding the "f" key gets really annoying then.
I did use love.keypressed for fullscreen toggling and put everything else (that requires key down) in love.update of course ^^
(just mis-typed keypressed into keyboardpressed but I did use keypressed)
Last edited by WolfosoBastardo on Wed Jul 06, 2011 3:40 pm, edited 1 time in total.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Weird Alpha, Layers, Sound and Rotate image to mouse pos

Post by vrld »

It's love.keypressed, not love.keyboardpressed.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], snibo and 3 guests