User Tools

Site Tools


en:tutorials:access_scrollclass_from_scrollobjects

Accessing the Scroll class from a ScrollObject

Being able to access common functions in the Scroll class is pretty essential in a Orx/Scroll based game. The Scroll class provides a singleton which can be accessed from anywhere in a ScrollObject class:

MyClass::GetInstance()

Imagine for a second that you had a function in your Scroll class to update a score. Collisions using OnCollide in the various instances of ScrollObject(s) might all need to update the score with different values.

Let's say the score function looks like this in the MyGame Scroll class:

void MyGame::AddToScore(int points) {
	score += points;
}

And say we had a Hero ScrollObject class which happened to collide with a Coin ScrollObject class:

orxBOOL Hero::OnCollide(ScrollObject *_poCollider,
	const orxSTRING _zPartName,
	const orxSTRING _zColliderPartName,
	const orxVECTOR &_rvPosition,
	const orxVECTOR &_rvNormal)
{
	const orxSTRING colliderName = _poCollider->GetModelName();
 
	if (orxString_Compare(colliderName, "Coin") == 0)
	{
		MyGame::GetInstance().AddToScore(100);
	}
 
	return orxTRUE;
}

The main point is that once the collision is detected, we can update the score in the Scroll class using the singleton:

MyGame::GetInstance().AddToScore(100);

That's all there is to it.

en/tutorials/access_scrollclass_from_scrollobjects.txt · Last modified: 2018/02/14 04:46 (7 months ago) by iarwain