User Tools

Site Tools


en:tutorials:orxscroll: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, 
                        orxBODY_PART *_pstPart, 
                        orxBODY_PART *_pstColliderPart, 
                        const orxVECTOR &_rvPosition, 
                        const orxVECTOR &_rvNormal)
{
 
	if (_poCollider == orxNULL) {
		return;
	}
 
	const orxSTRING colliderName = _poCollider->GetName();
 
	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/orxscroll/access_scrollclass_from_scrollobjects.txt · Last modified: 2024/11/06 04:50 (7 weeks ago) by sausage