Log In  

So I'm making a game that cycles through colors the sprite can turn into and I'm having a bit of trouble.

And this is the messy code:
if btn(4) then pal(12,2)
end
if btn(4) then pal(2,8)
end
if btn(4) then pal(8,12)
end
if btn(5) then pal(5,2)
end
if btn(5) then pal(2,8)
end
if btn(5) then pal(8,12)
end
if btn(5) then pal(12,5)
end

P#138611 2023-12-12 19:51

I think maybe I have an idea of what you're trying to do. Does this do more what you're thinking? Hold Z to cycle through the sprite square colors and the background rectangle colors, hold X to cycle through the sprite border colors, hold both to cycle both. The blue square with the grey border is sprite 1.

(This code is just thrown together, almost certainly more efficient ways of doing this. Also, don't run this if you are photosensitive!)

function _init()
    cti=1
    bti=1
    t=0
    c1={12,2,8}
    c2={2,8,12}
    c3={8,12,2}
    b1={2,8,12,5}
end

function _update()
    if btn(4) and t%5==0 then
        pal(12,c2[cti])
        pal(2,c3[cti])
        pal(8,c1[cti])
        cti=cti+1 > #c1 and 1 or cti+1
    end

    if btn(5) and t%5==0 then
        pal(5,b1[bti])
        bti=bti+1 > #b1 and 1 or bti+1
    end
    t+=1
end

function _draw()
    cls()
    rectfill(0,0,64,128,2)
    rectfill(64,0,128,128,8)
    spr(1,60,60)
end
P#138613 2023-12-12 21:21

There's 2 issues with your code from what I can tell:

  1. The pal() function doesn't change the current color with the number given. It changes the color that originally had that number. For the effect you described, you'd need to choose a single index and change it repeatedly. For example, if the starting color is color 12, the first parameter in all the calls to pal() should be 12.

  2. Every if statement in that sequence would happen one after another. As a result, you'd end up changing the color back to the original value every time. You need to keep track of the current value for that color and make sure only one of those pal() statements is run.

Edit: I always try to check if someone else answered while I was typing up an reply, but sometimes it doesn't work. Oh well. 2bitchuck's code shows the kind of structure you'd need. However I think that code assumed you were changing multiple colors each time.

P#138614 2023-12-12 21:21 ( Edited 2023-12-12 21:25)

What's the intended behavior? What specific trouble are you running into? FYI, Multiple expressions can be nested in a single if/then to save from having to repeat the same check:

if btn(4) then
 pal(12,2)
 pal(2,8)
 pal(5,2)
end
P#138615 2023-12-12 21:23

@kimiyoribaka Ha, I actually double-checked to see if Jason or you might have already answered since your answers tend to be better thought out than mine :).

P#138616 2023-12-12 21:26

[Please log in to post a comment]