我的游戏代码
#include"MyGame.h"
MyGameLayer::MyGameLayer()
{
total=0;
setIsTouchEnabled(true);
BallSprites=new int[150];
for(int i=0;i<150;i++)
BallSprites[i]=0;
CCSize s = CCDirector::sharedDirector()->getWinSize();
int setX=0;
int setY=0;
int size=40;
CCColorLayer *color = CCColorLayer :: layerWithColor(ccc4(255,255,255,255));
glColor4ub(128,128,128,255);
glLineWidth(20);
CCPoint kt[]={ CCPointMake(0,0),CCPointMake(40,0),CCPointMake(40,40),CCPointMake(0,40)};
ccDrawPoly(kt,4,false);
addChild(color,-1);
//加载网格图
/*CCSprite *sprite_bkimg=CCSprite::spriteWithFile(s_back3);
CCPoint pos(s.width/2,s.height/2);
sprite_bkimg->setPosition(pos);
addChild(sprite_bkimg);*/
//加载图像集
CCSpriteBatchNode *mgr=CCSpriteBatchNode::spriteSheetWithFile("Images/color_bean.png",120);
addChild(mgr,1,1314);
generate();
this->scheduleUpdate();
CCProgressTimer *timer=CCProgressTimer::progressWithFile("Images/r2.png");
timer->setType(kCCProgressTimerTypeRadialCCW);
timer->setPercentage(100);
timer->setPosition(CCPointMake(s.width/15*2+s.width/30,s.height/20+s.height/10*2));
timer->setScale(0.6);
addChild(timer,3,5022);
this->schedule(schedule_selector(MyGameLayer::timerset),1.5f);
}
void MyGameLayer::timerset(ccTime dt)
{
//CCLog("come to here");
CCProgressTimer *timer=(CCProgressTimer *)this->getChildByTag(5022);
int i=timer->getPercentage();
if(i==0)
{
this->unscheduleAllSelectors();
setIsTouchEnabled(false);
CCSize s = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Time's Up", "Thonburi", 34);
pLabel->setPosition(CCPointMake(s.width/2,s.height/2));
addChild(pLabel,5);
return;
}
timer->setPercentage(i-1);
}
void MyGameLayer::putBall(CCPoint pt,CCSprite *sprite)
{
sprite->setPosition(pt);
this->addChild(sprite);
}
CCPoint MyGameLayer::FreePistion(int m)
{
//隔5个找
CCSize s = CCDirector::sharedDirector()->getWinSize();
int i=0;
int ix=0;
int iy=0;
CCPoint p;
p.x=s.width/30;
p.y=s.height-s.height/20;
for( i=0;i<150;i+=5)
{
if(BallSprites[i]==0)
{
ix=i%15;
iy=i/15;
p.x+=ix*(s.width/15);
p.y-=iy*(s.height/10);
BallSprites[i]=m;
return p ;
}
}
//隔4个找
for( i=0;i<150;i+=4)
{
if(BallSprites[i]==0)
{
ix=i%15;
iy=i/15;
p.x+=ix*(s.width/15);
p.y-=iy*(s.height/10);
BallSprites[i]=m;
return p ;
}
}
//隔3个找
for( i=0;i<150;i+=3)
{
if(BallSprites[i]==0)
{
ix=i%15;
iy=i/15;
p.x+=ix*(s.width/15);
p.y-=iy*(s.height/10);
BallSprites[i]=m;
return p ;
}
}
//隔2个找
for( i=0;i<150;i+=2)
{
if(BallSprites[i]==0)
{
ix=i%15;
iy=i/15;
p.x+=ix*(s.width/15);
p.y-=iy*(s.height/10);
BallSprites[i]=m;
return p ;
}
}
//隔1个找
for( i=0;i<150;i+=1)
{
if(BallSprites[i]==0)
{
ix=i%15;
iy=i/15;
p.x+=ix*(s.width/15);
p.y-=iy*(s.height/10);
BallSprites[i]=m;
return p ;
}
}
return p;
}
void MyGameLayer::generate() //每次产生100个小球,共5
{
CCSpriteBatchNode *mgr=(CCSpriteBatchNode*)this->getChildByTag(1314);
float r;
int point=0;
for(int i=0;i<120;i++)
{
r=CCRANDOM_0_1();
if(r<0.2)
point=0;
else
if(r<0.4)
point=37;
else
if(r<0.6)
point=74;
else
if(r<0.8)
point=111;
else
point=148;
CCSprite *sprite_ball=CCSprite::spriteWithBatchNode(mgr,CCRect(point,18,37,46));
sprite_ball->setScale(0.7);
mgr->addChild(sprite_ball,0,i);
sprite_ball->setPosition(FreePistion(i*10+(point/37+1)));
}
}
void MyGameLayer::release()
{
// delete []BallSprites;
// BallSprites=NULL;
CCObject::release();
}
void MyGameLayer::update(ccTime dt)
{
//CCLog("here catch it");
if(isFinish())
{
this->unscheduleAllSelectors();
setIsTouchEnabled(false);
CCSize s = CCDirector::sharedDirector()->getWinSize();
//
std::string str("You Win! Score:");
total=0;
for(int ic=0;ic<150;ic++)
{
if (BallSprites[ic]!=0)
{
total++;
}
}
total=100-total;
CCProgressTimer *timer=(CCProgressTimer *)getChildByTag(5022);
total=total*0.6+timer->getPercentage()*0.4;
CCLog("total:%d",total);
char str2[50];
//itoa(total,str2,5);
//int temp=atoi(str2);
//CCLog(str2);
//
CCLog("%d",temp);
//
str.append(str2,5);
//const char *str1=str.c_str ();
sprintf(str2, "You Win! Score:%d", total);
CCLabelTTF* pLabel = CCLabelTTF::labelWithString(str2,"Thonburi", 34);
pLabel->setPosition(CCPointMake(s.width/2,s.height/2));
addChild(pLabel,5);
}
}
bool MyGameLayer::isFinish()
{
//150个位置判断是否有可消除位置
bool finish=true;//初始化已结束
int x=0,y=0;
int top=0,left=0,right=0,down=0;
for(int i=0;i<150;i++ )
{
if(BallSprites[i]!=0)continue;//如果不是空的格直接跳过
//否则 判断是否可消除路径
top=down=left=right=0;
x=i%15;
y=i/15;
//找到上面的位置
for(int i=y-1;i>=0;i--)
{
if(BallSprites[i*15+x]!=0)
{
top=BallSprites[i*15+x]%10;//上面的颜色
break;
}
}
//找到下面的位置
for(int i=y+1;i<10;i++)
{
if(BallSprites[i*15+x]!=0)
{
down=BallSprites[i*15+x]%10;
break;
}
}
//找到左边的位置
for(int i=x-1;i>=0;i--)
{
if(BallSprites[y*15+i]!=0)
{
left=BallSprites[y*15+i]%10;
break;
}
}
//找到右边的位置
for(int i=x+1;i<15;i++)
{
if(BallSprites[y*15+i]!=0)
{
right=BallSprites[y*15+i]%10;
break;
}
}
if(top==left&&top!=0)return false;
if(top==right&&top!=0)return false;
if(top==down&&top!=0)return false;
if(left==right&&left!=0)return false;
if(left==down&&left!=0)return false;
if(right==down&&right!=0)return false;
}
return true;
}
void MyGameLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
CCPoint location = touch->locationInView( touch->view() );
CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);
isDelable(convertedLocation);
}
bool MyGameLayer::isDelable(CCPoint pt)
{
//得到该位置是属于第几个
CCSize s = CCDirector::sharedDirector()->getWinSize();
int x=pt.x/(s.width/15);
int y=pt.y/(s.height/10);
y=9-y;
CCLog("%d %d",x,y);
//如果有球,则返回false
if(BallSprites[y*15+x]!=0)
return false;
//否则,从该位置往上检查
int top=-1,left=0,right=0,down=0,topi=0,lefti=0,righti=0,downi=0;
int tag1=-1,tag2=-1,tag3=-1,tag4=-1;//标志每个方向是否有要消除的小球
//向上数
for(int i=y-1;i>=0;i--)
{
if(BallSprites[i*15+x]!=0)
{
topi=i;
top=BallSprites[i*15+x];
break;
}
}
//向左数
for(int i=x-1;i>=0;i--)
{
if(BallSprites[y*15+i]!=0)
{
lefti=i;
left=BallSprites[y*15+i];
break;
}
}
//向右数
for(int i=x+1;i<15;i++)
{
if(BallSprites[y*15+i]!=0)
{
righti=i;
right=BallSprites[y*15+i];
break;
}
}
//向下数
for(int i=y+1;i<10;i++)
{
if(BallSprites[i*15+x]!=0)
{
downi=i;
down=BallSprites[i*15+x];
break;
}
}
CCSpriteBatchNode *mgr=(CCSpriteBatchNode *)getChildByTag(1314);
// 左 <--> 上 判断
if(left%10==top%10&&left!=0)
{
tag1=1;
tag2=1;
}
// 右 <--> 上 判断
if(top%10==right%10&&top!=0)
{
tag1=1;
tag3=1;
}
//下 <--> 上 判断
if(top%10==down%10&&top!=0)
{
tag1=1;
tag4=1;
}
// 左<-->右 判断
if(left%10==right%10&&left!=0)
{
tag2=1;
tag3=1;
}
// 左 <--> 下 判断
if(left%10==down%10&&left!=0)
{
tag2=1;
tag4=1;
}
//右 <--> 下 判断
if(right%10==down%10&&right!=0)
{
tag3=1;
tag4=1;
}
/*int * t1,*t2,*t3,*t4;
t1=new int;0
t2=new int;
t3=new int;
t4=new int;
*t1=topi*15+x;
*t2=y*15+lefti;
*t3=y*15+righti;
*t4=downi*15+x;
*/
if(tag1==1)
{
BallSprites[topi*15+x]=0;
this->runActionWithSprite((CCSprite *)mgr->getChildByTag(top/10),topi*15+x);
total++;
}
if(tag2==1)
{
BallSprites[y*15+lefti]=0;
this->runActionWithSprite((CCSprite *)mgr->getChildByTag(left/10),y*15+lefti);
total++;
}
if(tag3==1)
{
BallSprites[y*15+righti]=0;
this->runActionWithSprite((CCSprite *)mgr->getChildByTag(right/10),y*15+righti);
total++;
}
if(tag4==1)
{
BallSprites[downi*15+x]=0;
this->runActionWithSprite((CCSprite *)mgr->getChildByTag(down/10),downi*15+x);
total++;
}
if(tag1==-1&&tag2==-1&&tag3==-1)
{
CCProgressTimer *timer=(CCProgressTimer *)this->getChildByTag(5022);
int i = timer->getPercentage();
i=i-3;
if(i<=0)i=0;
timer->setPercentage(i);
if(i==0)
{
this->unscheduleAllSelectors();
setIsTouchEnabled(false);
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Time's Up", "Thonburi", 34);
pLabel->setPosition(CCPointMake(s.width/2,s.height/2));
addChild(pLabel,5);
}
}
return true;
}
void MyGameLayer::callback(CCNode* sender, void* data)
{
CCLog("%d",*((int *)data));
((CCSprite *) sender)->removeFromParentAndCleanup(true);
BallSprites[*((int *)data)]=0;
int *t=(int *)data;
delete t;
}
void MyGameLayer::runActionWithSprite(cocos2d::CCSprite *sprite,int pos)
{
CCAssert(sprite!=NULL,"i am sorry");
sprite->stopAllActions();
CCFiniteTimeAction * action1=CCBlink::actionWithDuration(0.2f,4);
CCFiniteTimeAction * action2 = CCFadeOut::actionWithDuration(1.0f);
int *temp=new int;
*temp=pos;
CCFiniteTimeAction * actionmore=CCSequence::actions(action1,action2, CCCallFuncND::actionWithTarget(this, callfuncND_selector(MyGameLayer::callback), (void*)temp),NULL);
sprite->runAction(actionmore);
}
void MyGameScene::runThisTest()
{
CCLayer* pLayer = new MyGameLayer();
addChild(pLayer);
pLayer->release();
CCDirector::sharedDirector()->replaceScene(this);
}
在运行时出现MyGame.obj : error LNK2019: 无法解析的外部符号 __imp__glLineWidth@4,该符号在函数 "public: __thiscall MyGameLayer::MyGameLayer(void)" (??0MyGameLayer@@QAE@XZ) 中被引用
1>MyGame.obj : error LNK2019: 无法解析的外部符号 __imp__glColor4ub@16,该符号在函数 "public: __thiscall MyGameLayer::MyGameLayer(void)" (??0MyGameLayer@@QAE@XZ) 中被引用
这样的错误,这是问什么啊?