[Help]Making boxes change textures
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
Hey, guys. I'm not comfortable for making a thread only to ask this, but I need to.
Well, the point is: I'm making a mod (and if you read my "Pop A Portal - Mappack" thread you should know), but this is my very first mod: "Mario +Portal"
I was trying to make boxes and buttons changing textures when colliding with each other. It was quite easy to do with buttons, but not with the boxes. I'm not expert in coding (in fact, I don't know almost anything about coding), but I would like a little help from you guys (and, of course, you'll be credited when I release my mod).
Pictures so you can understand what I'm talking about:
What happens actually:
(As you may see, the button already changes it's texture)
What I want to happen:
*The activated box texture inside main.lua:
boximageon = love.graphics.newImage("graphics/" .. graphicspack .. "/boxon.png")
and the image is:
Thanks for helping!
Well, the point is: I'm making a mod (and if you read my "Pop A Portal - Mappack" thread you should know), but this is my very first mod: "Mario +Portal"
I was trying to make boxes and buttons changing textures when colliding with each other. It was quite easy to do with buttons, but not with the boxes. I'm not expert in coding (in fact, I don't know almost anything about coding), but I would like a little help from you guys (and, of course, you'll be credited when I release my mod).
Pictures so you can understand what I'm talking about:
What happens actually:
(As you may see, the button already changes it's texture)
What I want to happen:
*The activated box texture inside main.lua:
boximageon = love.graphics.newImage("graphics/" .. graphicspack .. "/boxon.png")
and the image is:
Thanks for helping!
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
So... Anyone? I'm not asking you guys to make the mod/modded file for me. I just need a little help with the code. I tried making ifs - elses in the collision section, like:
if a == "button" then
self.graphic = boximageon
else
self.graphic = boximage
end
I've tried a few other things (in fact, I've spent almost 2 hours trying this and other ways), but none of them seems to work. So, I would really appreciate a llittle help. Thanks!
if a == "button" then
self.graphic = boximageon
else
self.graphic = boximage
end
I've tried a few other things (in fact, I've spent almost 2 hours trying this and other ways), but none of them seems to work. So, I would really appreciate a llittle help. Thanks!
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
I know, but I wanted to add it anyway. The buttons are also gonna change texture when activated in SE, but I made them myself. It is gonna be a graphic mod, with some retextured tiles and some new graphical entities. But I only got problems with the cube...lolpoop89 wrote:It's coming in SE. At least I think it is.
...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
Thanks! I'll try this and see how it works. I haven't even thought on adding the code inside button.lua. I've tried everything, but always inside the box.lua. Again, thanks!Qcode wrote:So this probably isn't the most efficient way of doing it, but I couldn't figure out using checkrect. It works anyway and you shouldn't see a drop in performance.
First go to button.lua, then button:update(dt).
At the end of that function you should seeAnywhere before that (in button:update) paste in this chunk of code.Code: Select all
if self.destroying then return true else return false end
That should give the desired effect.Code: Select all
local on self.graphic = boximage for i, v in pairs(objects["button"]) do if aabb(self.x, self.y, 1, 1, v.x, v.y, 1.875, .1875) then on = true end end if on then self.graphic = boximageon end
...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
Qcode wrote:Wait no sorry.
All of that I meant with box.lua.
Should be in box.lua, and should be in box:update.
All the code in[/spoiler]Code: Select all
tags should remain the same though. Completely my fault there.[/quote] No problems, I put it in box.lua. Also, I replaced "on" by "toggled", because it's easier to me. So, should it look like this? [spoiler][code] box = class:new() function box:init(x, y) --PHYSICS STUFF self.cox = x self.coy = y self.x = x-14/16 self.y = y-12/16 self.speedy = 0 self.speedx = 0 self.width = 12/16 self.height = 12/16 self.static = false self.active = true self.category = 9 self.parent = nil self.portaloverride = true self.mask = { true, false, false, false, false, false, false, true, false, true, true, false, false, true, false, false, true, true, false, false, true, false, true, true, false, false, true, false, true, true, true} self.emancipatecheck = true self.userect = adduserect(self.x, self.y, 12/16, 12/16, self) --IMAGE STUFF self.drawable = true self.quad = boxquad self.offsetX = 6 self.offsetY = 2 self.quadcenterX = 6 self.quadcenterY = 6 self.rotation = 0 --for portals self.falling = false self.destroying = false self.outtable = {} self.portaledframe = false end function box:update(dt) local toggled self.graphic = boximage for i, v in pairs(objects["button"]) do if aabb(self.x, self.y, 1, 1, v.x, v.y, 1.875, .1875) then toggled = true end end if toggled then self.graphic = boximageon end local friction = boxfrictionair if self.falling == false then friction = boxfriction end if not self.pushed then if self.speedx > 0 then self.speedx = self.speedx - friction*dt if self.speedx < 0 then self.speedx = 0 end else self.speedx = self.speedx + friction*dt if self.speedx > 0 then self.speedx = 0 end end else self.pushed = false end --rotate back to 0 (portals) self.rotation = math.mod(self.rotation, math.pi*2) if self.rotation > 0 then self.rotation = self.rotation - portalrotationalignmentspeed*dt if self.rotation < 0 then self.rotation = 0 end elseif self.rotation < 0 then self.rotation = self.rotation + portalrotationalignmentspeed*dt if self.rotation > 0 then self.rotation = 0 end end if self.parent then local oldx = self.x local oldy = self.y self.x = self.parent.x+math.sin(-self.parent.pointingangle)*0.3 self.y = self.parent.y-math.cos(-self.parent.pointingangle)*0.3 if self.portaledframe == false then for h, u in pairs(emancipationgrills) do if u.dir == "hor" then if inrange(self.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, oldy, self.y, true) then self:emancipate(h) end else if inrange(self.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, oldx, self.x, true) then self:emancipate(h) end end end end end self.userect.x = self.x self.userect.y = self.y --check if offscreen if self.y > 17 then self:destroy() end self.portaledframe = false if self.destroying then return true else return false end end function box:leftcollide(a, b) if a == "button" then self.y = b.y - self.height self.x = b.x + b.width - 0.01 if self.speedy > 0 then self.speedy = 0 end return false elseif a == "player" then self.pushed = true return false end end function box:rightcollide(a, b) if a == "button" then self.y = b.y - self.height self.x = b.x - self.width+0.01 if self.speedy > 0 then self.speedy = 0 end return false elseif a == "player" then self.pushed = true return false end end function box:floorcollide(a, b) if self.falling then self.falling = false end if a == "goomba" or a == "bulletbill" then b:stomp() addpoints(200, self.x, self.y) playsound(stompsound) self.falling = true return false end end function box:passivecollide(a, b) if a == "player" then if self.x+self.width > b.x+b.width then self.x = b.x+b.width else self.x = b.x-self.width end end end function box:startfall() self.falling = true end function box:emancipate() if self.parent then self.parent:cubeemancipate() end self:destroy() end function box:destroy() self.userect.delete = true self.destroying = true for i = 1, #self.outtable do if self.outtable[i].input then self.outtable[i]:input("toggle") end end end function box:addoutput(a) table.insert(self.outtable, a) end function box:used(id) self.parent = objects["player"][id] self.active = false objects["player"][id]:pickupbox(self) end function box:dropped() self.parent = nil self.active = true end function box:portaled() self.portaledframe = true end
...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.
- HugoBDesigner
- Posts: 2189
- Joined: 19 Sep 2012, 02:23
- Contact:
Well, it worked pretty well, but then I found a problem: you were calculating the box changing textures through x and y axis, but they aren't precise (I've changed the values to some small decimal number, but they keep being imprecise). And it, also, changes it texture even if the cube is parented to the player (and makes the process less... "realistic"). I'll keep using your code until I find something different to work with or until I give up. Thanks!Qcode wrote:Yep that's how it should look.