An enumeration class is defined in the. h file, which contains the upper and lower left and right four directions.
Enum DIR
{
Right_dir,
Left_dir,
Up_dir,
Down_dir
}
Like the snake game, is the automatic movement, is to define a scheduled task schedule, how many seconds to move up and down about how much. However, you should first define the interaction event.
Creating Listener Objects
Auto Listener=eventlistenertouchonebyone::create ();
callback method
Listener->ontouchbegan=cccallback_2 (Gamescene::gamelogic,this);
Event distribution
_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener,this);
--------------------------------------------------------------------------------------------------------------- -----------------------
if (mx>my)//move around
{
// The direction of the click is larger than the direction of the head, moving to the right, horizontally, or left.
if (Touch->getlocation (). x-10>head->col*31)
{
//Move right
head->dir=right_dir;
}
else
{
Move left
head->dir=left_dir;
}
}
else//Move up and down
{
if (Touch->getlocation (). y-10>head->row*31)
{
Move Up
head->dir=up_dir;
}
Else
{
Move Down
head->dir=down_dir;
}
}
The above should be written into the Ontouchbegan function, due to the time of the small series, not to elaborate, I hope to be useful to you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Iron Mouth Lee's COCOS2DX tour of some games in the direction of control tips