Search
  • Justin Halek

Pixel perfect movement in GML

Updated: Oct 8

Pixel perfect movement in GML is often a tricky thing for new programmers to get correct. Here is an example of pixel perfect movement, Written by Wubs, and edited by Wansou check out the code right after this gif:

The code:



function pixelMovement(_hspd,_vspd){
	// horizontal movement first
    if place_meeting(x+sign(_hspd),y,parSolid) and _hspd != 0 {
        repeat(abs(_hspd)){
            if place_meeting(x+sign(_hspd),y,parSolid)=false{
                x+=sign(_hspd)
            }else{
                _hspd=0
                break
            }
        }
    }else {
        x+=_vspd
    }
	
	// vertical movement
    if place_meeting(x,y+sign(_vspd),parSolid) and _vspd != 0{
        repeat(abs(_vspd)){
            if place_meeting(x,y+sign(_vspd),parSolid)=false{
                y+=sign(_vspd)
            }else{
                _vspd=0
                break
            }
        }
    }else{
        y+=_vspd
    }
}

20 views0 comments