Hola all,
I've been working on a game with my son, using Baxslashes Boxman demo as its base. I've run into problems making the player sprite switch between two sets of frames for running and jumping. They are all on one sprite sheet.
Problem 1, line 84 - if the sprite is moving, why do I need to put the PlaySprite after the else command? The animation should only play when the character is not moving?
Problem 2, line 134 - why does running the PlaySprite command to show the jump animation frames inhibit the physics from jumping properly?
rem
rem AGK Application
rem
rem a Baxslash originally did it!
SetVirtualResolution (640, 960)
setSyncRate(60,0)
SetPhysicsDebugOn()
SetPhysicsScale( 0.02 )
SetPhysicsGravity( 0, 700 )
SetClearColor( 0, 0, 64 )
#constant platforms = 1
#constant myfloor = 2
#constant vent = 3
#constant entities = 4
#constant player = 5
global boxman BoxmanVel kinematic direction paused as integer
global AtlasIMG iPreviousImageCount as integer : AtlasIMG = 12000
type animtype
iImageID as integer
iImage as integer
sName as string
endtype
SetPhysicsWallLeft(1)
SetPhysicsWallRight(1)
//load the player animation frames
spr = Setup_Atlas("WalkJumpNoItem")
setSpriteOffset (spr, getSpriteWidth(spr)/2, getSpriteHeight(spr))
setSpriteVisible (spr, 1)
setspritedepth (spr, 20)
setSpritePhysicsOn (spr, 2)
setSpritePhysicsCanRotate (spr, 0)
setSpritePhysicsMass (spr, 200.32)
SetSpriteCategoryBit (spr, platforms, 1 )
SetSpriteCategoryBit (spr, entities, 0 )
SetSpriteCategoryBit (spr, player, 1 )
SetSpriteCollideBit (spr, myfloor, 1 )
SetSpriteCollideBit (spr, vent, 0 )
boxman = spr
gosub CreateLevel
do
playerControl()
playerJump()
keyboardInput()
updatePlatforms()
if paused = 1
stepPhysics(0)
endif
Sync()
loop
function playerControl()
if GetRawKeyState(65) = 1 `'A' key
BoxmanVel = -330
SetSpriteFlip (boxman, 1, 0 )
elseif GetRawKeyState(68) = 1 `'D' key
BoxmanVel = 330
SetSpriteFlip (boxman, 0, 0 )
else
BoxmanVel = 0
endif
if BoxmanVel <= -1 or BoxmanVel => 1
//animation loops only when boxman is moving (BoxmanVel moving left -1 or right 1)
//this is WIERD. Why does the walk animation play if it fails the velocity check?
//and when not moving, why does the animation jerk as if playing the first frame and failing?
else
PlaySprite(boxman, 48, 1, 1, 52 )
endif
rem move boxman
setSpritePhysicsVelocity(boxman, BoxmanVel, getSpritePhysicsVelocityY(boxman))
endfunction
function playerJump()
leftedge = 0
rightedge = 0
bX = getSpriteX(boxman)
bh = getSpriteHeight(boxman)
bY = getSpriteY(boxman) +bh/2
bw = getSpriteWidth(boxman) ///2
//if he's falling (positive value), turn on the physics to not pass through platforms
if GetSpritePhysicsVelocityY(boxman) > 1 then SetSpriteCollideBits (boxman, %10011) //platforms on
//rightEdge
//DrawLine(bX, by, bX -8, by+bh, 255, 0, 0)
//leftEdge
//DrawLine(bX +bw, by, bX +bw +8, by+bh, 255, 0, 0)
//fire two rays off the both vertical edges downwards. Allows jumping when on the edge of platforms.
rightEdge = PhysicsRayCastCategory(%11, bX , bY, bX -8, bY+bh)
leftEdge = PhysicsRayCastCategory(%11, bX +bw, bY, bX +bw +8, bY+bh)
if GetPointerPressed() > 0 or GetRawKeyPressed(32) = 1//and leftEdge > 0 OR rightEdge > 0
SetSpriteCollideBits (boxman, %00010) //platforms off
jump = 1
if leftEdge > 0 OR rightEdge > 0
setSpritePhysicsVelocity(boxman, getSpritePhysicsVelocityX(boxman), -jump*650)
if getSpritePlaying(boxman) = 1
stopSprite(boxman)
endif
//WIERD number 2. Why does this PlaySprite command stop the physics jump? Comment it out to jump normally (but not play the jump animation)
PlaySprite(boxman, 48, 0, 53, 104 )
endif
endif
endfunction
function updatePlatforms()
rem update kinematic platform
setSpritePhysicsVelocity(kinematic,0, direction)
if getSpriteYbyOffset(kinematic)>800
direction=-150
else
if getSpriteYbyOffset(kinematic)<200
direction=150
endif
endif
endfunction
function Setup_Atlas(sTexturename as string)
//get the name of the texture sheet, derive "texturesheetname subimages" from it. Count how many subimages are mentioned (MaxLines)
sPath as string : sPath = sTexturename + " subimages.txt"
fileid = OpenToRead(sPath)
MaxLines = 0
while FileEOF(fileid) = 0
inc MaxLines, 1
currentline$ = ReadLine(fileid)
endwhile
CloseFile(fileid)
//open the file again but this time get the image name from each line. Split line and get segment 0, the part before the '.' in .png
dim animData[Maxlines] as animtype
fileid = OpenToRead(sPath)
for i = 1 to MaxLines -1 //-1 because last line = EoF
currentline$ = ReadLine(fileid)
string$ = str_split(currentline$, ".", 0) //get first segment
animData[i].sName = string$
next i
CloseFile(fileid)
inc animcount
dim anim[animcount, MaxLines] as animtype
//increment the atlas ID in case this is not the only animation to be loaded, load the spritesheet, create the sprite
AtlasIMG = AtlasIMG + iPreviousImageCount
LoadImage( AtlasIMG, sTexturename + ".png" )
mySprite = CreateSprite(AtlasIMG)
//grab subimage from spritesheet, add frames to sprite
for i = 1 to MaxLines -1
anim[animcount,i].iImageID = AtlasIMG + i
anim[animcount,i].sName = animData[i].sName + ".png"
LoadSubImage (anim[animcount,i].iImageID, AtlasIMG, anim[animcount,i].sName )
AddSpriteAnimationFrame (mySprite,anim[animcount,i].iImageID)
next i
iPreviousImageCount = i
undim anim[]
undim animData[]
endfunction mySprite
function keyboardInput()
//P to Pause
if getrawkeystate(80) = 1
if paused = 1
paused = 0
else
paused = 1
endif
endif
if GetRawKeyState( 27 ) = 1 `'esc' key
end
endif
endfunction
function str_split(pString as string, pDelimiter as string, pSegment)
delimitCount as integer
myReturn as string
myReturn = ""
charCount as integer
segmentCount as integer
exitSearch as integer
i as integer
pStringLength as integer
pStringLength = len(pString)
for i = 0 to pStringLength - 1
if mid(pString, i + 1, 1) = pDelimiter
delimitCount = delimitCount + 1
endif
next i
if delimitCount = 0 or pSegment < 0 or pSegment > delimitCount
exitfunction pString
endif
if pSegment > 0
for i = 0 to pStringLength - 1
charCount = charCount + 1
if mid(pString, i + 1, 1) = pDelimiter
segmentCount = segmentCount + 1
if segmentCount = pSegment
exit
endif
endif
next i
endif
if charCount >= pStringLength
myReturn = ""
exitfunction myReturn
endif
while exitSearch = 0 and charCount < pStringLength
if mid(pString, charCount + 1, 1) = pDelimiter
exitSearch = 1
else
myReturn = myReturn + mid(pString, charCount + 1, 1)
charCount = charCount + 1
endif
endwhile
endfunction myReturn
CreateLevel:
rem create static platforms FLOOR
spr = createSprite(0)
setSpriteSize(spr,640,100)
setSpritePosition(spr,0,940)
setSpritePhysicsOn(spr,1)
SetSpriteCategoryBit( spr, platforms, 0 )
SetSpriteCategoryBit( spr, myfloor, 1 )
SetSpriteCategoryBit( spr, vent, 0 )
SetSpriteCategoryBit( spr, entities, 0 )
spr = createSprite(0) //RED
setSpriteSize(spr,100,20)
setSpriteColor(spr, 255,0,0,255)
setSpritePosition(spr,40,700)
setSpritePhysicsOn(spr,1)
SetSpriteCategoryBit( spr, platforms, 1 )
SetSpriteCategoryBit( spr, myfloor, 0 )
SetSpriteCategoryBit( spr, vent, 0 )
SetSpriteCategoryBit( spr, entities, 0 )
spr = createSprite(0) //BLUE VENT
setSpriteSize(spr,100,20)
setSpriteColor(spr, 0,0,255,255)
setSpritePosition(spr,500,600)
setSpritePhysicsOn(spr,1)
SetSpriteCategoryBit( spr, platforms, 0 )
SetSpriteCategoryBit( spr, myfloor, 0 )
SetSpriteCategoryBit( spr, vent, 1 )
SetSpriteCategoryBit( spr, entities, 0 )
rem create a kinematic platform
spr = createSprite(0)
setSpriteSize(spr,100,20)
setSpritePosition(spr,320,800)
setSpritePhysicsOn(spr,3)
SetSpriteCategoryBit( spr, platforms, 1 )
SetSpriteCategoryBit( spr, myfloor, 0 )
SetSpriteCategoryBit( spr, vent, 0 )
SetSpriteCategoryBit( spr, entities, 1 )
kinematic = spr
direction = 150
return
Project with spritesheet attached. Any help appreciated!