  | |  | smooth scrolling/subpixel tweening | smooth scrolling/subpixel tweening 2003-11-23 - By Robert Tweed
Back A few pointers:
1 - Don't use updateStage, ever.
2 - Don't animate in repeat loops. Use frame events instead. Use enterFrame or stepFrame to do the majority of your processing, as these events get the biggest CPU timeslice.
3 - To animate in fractions of pixels, simply store the position in a property, which can have fractional values. For example, use pLoc for the loc, then at some point do pSprite.loc = pLoc. The sprite loc will be converted to an integer value, but internally, you will still be working with floating point.
Wherever possible, avoid complex formulae like this:
> pY = (-0.32 * power(abs(pX - 5), 2)) + pB
Try to stick to simple addition and subtraction as much as possible. To do a jump, simply use values that represent force vectors. Here's an example of some simple platformer code (needs some blanks filled in) - notice simple the mathematical stuff is:
property pSprite, pLoc, pDir, pOnFloor global gLeftKey, gRightKey, gJumpKey global gGravity -- e.g., point( 0, 0.5 )
on beginSprite me pSprite = sprite( me.spriteNum ) pLoc = pSprite.loc pDir = point( 0, 0 ) pOnFloor = false end
on prepareFrame me h = 0 if( keyPressed( gRightKey ) ) then h = h + 4 if( keyPressed( gLeftKey ) ) then h = h - 4 pDir.locH = h if( keyPressed( gJumpKey ) ) then me.jump() end
on jump me if( pOnFloor <> true ) then return pDir.locV = -7 end
on enterFrame me pOnFloor = false pDir = pDir + gGravity newLoc = pLoc + pDir pLoc = me.checkCollisions( newLoc ) pSprite.loc = pLoc end
on checkCollisions me, newLoc if( me.hitFloor( pLoc, newLoc ) ) then newLoc = me.intersectWithFloor( pLoc, newLoc ) pDir = point( 0,0 ) pOnFloor = true end if return newLoc end
on hitFloor me, loc1, loc2 -- Check to see if hit the floor end
on intersectWithFloor me, loc1, loc2 -- Return point where vector instersects floor end
- Robert
__ ____ ____ ____ ____ ____ ____ ____ ____ ____ dirGames-L mailing list - dirGames-L@(protected) http://nuttybar.drama.uga.edu/mailman/listinfo/dirgames-l
Earn $52 per hosting referral at Lunarpages.
|
|
 |