AppGameKit Classic

AppGameKit Classic

Otillräckligt med betyg
AppGameKit Sprite Collision - A basic guide
Av thescenecommander
Another of my guides aimed at getting started with coding with AGK2. I've had a few requests for some more information on collision, so this guide aims to cover the basics.
   
Utmärkelse
Favorit
Favoritmarkerad
Avfavoritmarkerad
Basic Sprite Collision.
Hi and welcome to another of my AppGameKit coding guides. As before these are aimed at the beginner and the code is well documented. Keeping to the original format of my previous guides, I've attached a video of what you should see if all goes well.


As before, you should create a new project as described in one of the previous tutorials. Also, sticking to a theme, the media used is the same balloon media provided with AppGameKIt, so don't forget to transfer the required files into your projects folder. Again, details of how to do this are covered in an earlier guide.

If you've been following these guides, you'll noticed that I've expanded on the code used in the guide Animating a Sprite, and you may like to compare the two sets of code to see what changes have been made.

So, here's the code, remember you can cut and paste this into your AppGameKit IDE and click run to see it in action.

If you've any questions please ask, and I hope you find this useful.

// Project: Checking for sprite collisions, a basic demo.
// Created: 2014-11-21

// set window properties
SetWindowTitle( "Sprite Collision" )

// 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

// Set up player data, in this case, we'll be attaching a player balloon to the pointer so all we need is a sprite number.

playersprite=2


// 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"))

// We want to use the same sprite for the player sprite, so we'll look at another command here, that allows the quick duplication of exisiting sprite.

// Because this is a collision demonstration, we'll look at several collision modes, I'll remmed some out using // but I do encourage you to try other modes. By default
// all collision is created as a simple box, this is the fastest and most efficient mode, but isn't the most accurate. It's suitable for
// most purposes but more complex shapes may benefit from other modes. If you are intending to use box collision there's no need to set
// it as it is the default, but to allow you to play with the settings, I've included it.

SetSpriteShape(balloonsprite,3) // Set the sprite to polygons (mode 3), this is the most accurate and creates a collision area based on the sprite image. It is the mode we'll
// be using as a default for this demo
//SetSpriteShape(balloonsprite,2,spriteheight(balloonsprite)/2,Spritewidth(balloonsprite)/2) // Set the sprite to have a circle collision
// the sprite height and width are used to find the centre of the image.

//SetSpriteShape(balloonsprite,1) // Sets the sprite to a simple box collision, this is the default and precalculates a collision box based on the sprite size


// Make a duplicate of balloonsprite and assign it and it's values to playersprite. This only copies sprite details and not any physics details. Collision modes
// are copied over.

CloneSprite(playersprite,balloonsprite)

// Set up both sprites to animated
PlaySprite (balloonsprite)

PlaySprite(playersprite)


// Main loop

do

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

// Get the pointer position (this could be a mouse pointer on a PC or Mac or a virtual pointer, e.g. a screep tap or swipe on another device.).
// For ease of use, we're going assign the pointer x and y location to variables.

playerx=GetPointerX()
playery=GetPointerY()

// Set the position of the playersprite at the position indicated by those variables. Being offset from the top right corner as normal.

SetSpritePosition(playersprite,playerx,playery)

// There are several different ways of returning collision data, for this guide, we'll just look at comparing playersprite and ballonsprite

if GetSpriteCollision(playersprite,balloonsprite)=1 // Have the 2 sprites collided?
// If so, print a basic message. - We will expand on this at a later date.
Print ("Sprites have collided")
endif

//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