AppGameKit Classic

AppGameKit Classic

评价数不足
Flipping a sprite.
由 thescenecommander 制作
This guide expands on the previous sprite movement guide by providing and demonstrating documented code, showing simple sprite flipping in action and expanding on the sprite movement routine to demonstrate the effect of bouncing around the screen.
   
奖励
收藏
已收藏
取消收藏
Sprite flipping and addtional movement.
Hi,

Continuing on the theme of sprite movement, this small code sample expands on the previous sprite move demo by showcasing the SetSpriteFlip command. I've included a short video of the code in action so users can see results, but I encourage you all to cut and paste the code the sample into your project and play around with the code.

As before, the code is aimed at the very first time user.

// Project: making a sprite move.
// Created: 2014-11-21

// set window properties
SetWindowTitle( "making a sprite move." )

// set variables
screenwidth=1024
screenheight=768
fullscreen=0

// set display properties for platforms that support windows, e.g., Windows or Mac.

SetWindowSize(screenwidth,screenheight,fullscreen )

// set display properties for mobile devices.

SetVirtualResolution(screenwidth,screenheight)


// Set up a sprite data

balloonsprite=1
balloonx=512
balloony=384
balloondirectionX=1
balloondirectionY=1


// create a sprite with ID that uses the balloon image
CreateSprite (balloonsprite,0)

AddSpriteAnimationFrame(balloonsprite,LoadImage("item0.png"))
AddSpriteAnimationFrame(balloonsprite,LoadImage("item1.png"))
AddSpriteAnimationFrame(balloonsprite,LoadImage("item2.png"))
AddSpriteAnimationFrame(balloonsprite,LoadImage("item3.png"))
AddSpriteAnimationFrame(balloonsprite,LoadImage("item4.png"))

PlaySprite (balloonsprite)

// Main loop

do

// set the sprite position.
SetSpritePosition (balloonsprite,balloonx,balloony)

//move the balloon along the X axis by the amount specified by balloondirection
balloonx=balloonx+balloondirectionX
//move the balloon along the Y axis by the amount specified by balloondirection
balloony=balloony+balloondirectionY
// check to see if the balloon is near the far right edge of the screen and if so, reverse direction
if balloonx+GetSpriteWidth(balloonsprite)>screenwidth
balloondirectionX=-1
// Reverse sprite on the x bias based on the original orientation, you can also flip on the Y bias by changing the second value.
SetSpriteFlip(balloonsprite,1,0)
endif

// check to see if the balloon is near the far left edge of the screen and if so, reverse direction
if balloonx<1
balloondirectionX=1
// Reset sprite on the x bias based on the original orientation, you can also flip on the Y bias by changing the second value.
SetSpriteFlip(balloonsprite,0,0)
endif

// check to see if the balloon is near the top edge of the screen and if so, reverse direction
if balloonY<1
balloondirectionY=1
endif

// check to see if the balloon is near the bottom edge of the screen and if so, reverse direction
if balloonY+GetSpriteHeight(balloonsprite)>screenheight
balloondirectionY=-1
endif


Sync()
loop