Simple Run Cool

Source: Internet
Author: User

Define a direction enumeration (header file)
typedef enum JUMPDIR
{
Dirup,dirdown,dirstop
}
Public
int speed;//Speed
Jumpdir Jump;
CPP file ************
1.
Size visiblesize = director::getinstance ()->getvisiblesize ();
VEC2 origin =

Director::getinstance ()->getvisibleorigin ();

Speed = 30;//initialized to 30

2.//Adding a background layer (anchor points default at point (0.5,0.5))
Auto BG = sprite:create ("bg.jpg");
Bg.setposition (VISIBLESIZE/2);
Bg.settag (90);//Set a tag
This->addchild (BG);

3.//loading the map layer and adding the created map to the game (the anchor point is set to 0,0)
Auto MAP01 = tmxtiledmap::create ("t01.tmx");

Map1->settag (100);
Set a tag
This->addchild (MAP1);

4. Create a sprite animation and let it run on the map. (Can do a package)
Auto cache = Spriteframecache::getinstance ();

Cache->addspriteframeswithfile ("Run.plist", "run.png");

Spriteframe *frame;

Vector<spriteframe*> Arrayframe;

int index = 1;

Do
{

frame = Cache->spriteframebyname (String::createwithformat ("Run%d.png", Index)->getcstring ());


if (frame = = NULL)

{

Break

}

Arrayframe.pushback (frame);

index++;

} while (1);

animation* Animation = Animation::createwithspriteframes (arrayframe);

Animation->setdelayperunit (0.1f);

Animation->setloops (-1);

animate* act = animate::create (animation);
Sp->runaction (ACT);

5. In the update function, scroll the map infinitely
Get map Markers
Auto MAP01 = This->getchildbytag (100);
if (Map01->getpositionx () > -16000+960)//map01->getpositionx () Gets a point that moves to the left less than 0
{
Map01->setpositionx (Map01->getpositionx ()-5);
}


6. Add Touch Events

Handle the jumps of the characters

7. Encapsulate a character jumping function
void Spritejump ();
{
Character jumping

Auto SP = This->getchildbytag (120);

if (jump = = Jumpdir::D Irup)
If the direction is upward, otherwise the direction is downward
{//Make the sprite move upward similar to the stone that is being thrown upwards, the speed is smaller, the velocity is less than 0 and falling
Sp->setpositiony (Sp->getpositiony () +speed);
The Sprite's y-axis decrements from 30
Speed = speed-2;
2 reduction in speed per frame
if (speed==0)
When the ascent speed is less than 0 o'clock, change the direction of movement downward
{

Jump=jumpdir: The direction of movement of the:D irdown;//downward
}

Log ("dirup=%d", speed);

}else if (jump==jumpdir::D irdown)
If the direction is upward, otherwise the direction is downward
{

Sp->setpositiony (Sp->getpositiony ()-speed);//The y-axis of the sprite increases from 0 onwards
Speed = speed + 2;
2 increase in speed per frame
Log ("dirdown=%d", speed);

Checkdown ();
Check if you stepped on the lawn.
if (Speed > 30)

{

Jump = Jumpdir::D irstop;
Movement Direction Stop
Speed = 30;

Checkdown ();

}
}
}
-----------------------------
void Helloworld::checkdown ()

{
Auto Nowplayer = (sprite*) this->getchildbytag (120);

Auto Nowmap = (tmxtiledmap*) this->getchildbytag (100);

PX Scrolling map length plus the character's position on the screen is where the character treads on the map.
int px = Nowplayer->getpositionx () +abs (0-nowmap->getpositionx ());

The location of the py characters in the map
int py = Nowplayer->getpositiony () +0;

People in the ranks of the map


int nowrow = PX/32;
Lines of the tiled map
int nowcol = PY/32;
Columns of the tiled map
Log ("nowrow=%d nowcol=%d", Nowrow,nowcol);

if (Nowcol < 0 | | Nowcol >14)

{
return;
}


(Height of map/block size-Nowcol)
int tid = Nowmap->getlayer ("t01")->gettilegidat (VEC2 (Nowrow,14-nowcol));

Log ("tid=%d", Tid);

if (Tid >0)

{

Jump = Jumpdir::D irstop;

}else

{

Log ("Game ocer");

Return

}



}



This article is from "A grain of sand in the Wind" blog, please be sure to keep this source http://libinqi456.blog.51cto.com/4819343/1606107

Simple Run Cool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.