1. Can't Get Sprite To Stay In Screen Bounds

Hello all,

I am playing aroun with one of my old Sigil demos. I was trying to make it so you can move the sprite around using the arrow keys, which I have accomplished. However I can't keep the sprite from going off the screen. Here is my code. Any help is appericated.

without type_check 
 
include std/machine.e 
include EuSigil.ew 
 
constant WIDTH = 640, HEIGHT = 480 
 
slWindow(WIDTH,HEIGHT,"Sprite Example",0) 
 
atom x = WIDTH / 2 
atom y = HEIGHT / 2 
 
integer spr = slLoadTexture("Stick.png") 
 
if spr = -1 then 
	puts(1,"Failed to load sprite!\n") 
	abort(0) 
end if 
 
while not slShouldClose() do 
 
    slSetBackColor(255,255,255) --change background to white 
 
	if slGetKey(SL_KEY_ESCAPE) = 1 then 
		slClose() 
	end if 
	 
	if slGetKey(SL_KEY_LEFT) = 1 then 
		x -= 2 
		slSprite(spr,x,y,32,32) 
		elsif x  and WIDTH <= 0 then --sprite shouldn't be able to go off the screen? 
			x *= 2 
			slSprite(spr,x,y,32,32) 
	end if 
	 
    slSprite(spr,x,y,32,32) 
	 
   slRender() 
    
end while 
 
slClose() 
new topic     » topic index » view message » categorize

2. Re: Can't Get Sprite To Stay In Screen Bounds

Should be something like this:

if slGetKey(SL_KEY_LEFT) = 1 then 
    x -= 2 
    if x < 0 then 
        x = 0 
    end if 
    slSprite(spr,x,y,32,32) 
end if 

See if this works:

while not slShouldClose() do  
 
    slSetBackColor(255,255,255) --change background to white  
 
    if slGetKey(SL_KEY_ESCAPE) = 1 then  
        slClose()  
    end if  
 
    if slGetKey(SL_KEY_LEFT) = 1 then  
        x -= 2  
        if x < 0 then 
            x = 0 
        end if 
    end if  
 
    if slGetKey(SL_KEY_RIGHT) = 1 then  
        x += 2  
        if x > (WIDTH-32) then 
            x = WIDTH-32 
        end if 
    end if  
 
    slSprite(spr,x,y,32,32)  
 
    slRender()  
 
end while  
new topic     » goto parent     » topic index » view message » categorize

3. Re: Can't Get Sprite To Stay In Screen Bounds

euphoric said...

Should be something like this:

if slGetKey(SL_KEY_LEFT) = 1 then 
    x -= 2 
    if x < 0 then 
        x = 0 
    end if 
    slSprite(spr,x,y,32,32) 
end if 

See if this works:

while not slShouldClose() do  
 
    slSetBackColor(255,255,255) --change background to white  
 
    if slGetKey(SL_KEY_ESCAPE) = 1 then  
        slClose()  
    end if  
 
    if slGetKey(SL_KEY_LEFT) = 1 then  
        x -= 2  
        if x < 0 then 
            x = 0 
        end if 
    end if  
 
    if slGetKey(SL_KEY_RIGHT) = 1 then  
        x += 2  
        if x > (WIDTH-32) then 
            x = WIDTH-32 
        end if 
    end if  
 
    slSprite(spr,x,y,32,32)  
 
    slRender()  
 
end while  

Thanks, it worked!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu