您的位置:首页 > 移动开发 > Cocos引擎

在PC上运行的Cocos2d-x for XNA “Hello world”

2012-03-14 23:57 417 查看
编译好的DLL文件下载地址:http://download.csdn.net/detail/qy1213/4141611

1、新建项目



2、添加DLL引用



3、编写两个类AppDelegate.cs和HelloWorldScene.cs

public class AppDelegate : CCApplication
{
public AppDelegate(Game game, GraphicsDeviceManager graphics)
: base(game, graphics)
{
CCApplication.sm_pSharedApplication = this;
}

public override bool initInstance()
{
return base.initInstance();
}

public override bool applicationDidFinishLaunching()
{
CCDirector pDirector = CCDirector.sharedDirector();
pDirector.setOpenGLView();
pDirector.DisplayFPS=true;
pDirector.animationInterval = 1.0 / 60;
CCScene pScene = HelloCocos2dScene.scene();
pDirector.runWithScene(pScene);

return true;
}

public override void applicationDidEnterBackground()
{
CCDirector.sharedDirector().pause();
}

public override void applicationWillEnterForeground()
{
CCDirector.sharedDirector().resume();
}
}


public class HelloCocos2dScene : CCLayer
{
public override bool init()
{
CCDirector.sharedDirector().deviceOrientation = ccDeviceOrientation.CCDeviceOrientationLandscapeLeft;
if (!base.init())
{
return false;
}

this.m_bIsTouchEnabled = true;
CCMenuItemImage pCloseItem = CCMenuItemImage.itemFromNormalImage(
"CloseNormal",
"CloseSelected",
this,
new SEL_MenuHandler(menuCloseCallback) );
pCloseItem.position = new CCPoint(CCDirector.sharedDirector().getWinSize().width - 20, 20);
CCMenu pMenu = CCMenu.menuWithItems(pCloseItem);
pMenu.position = new CCPoint(0, 0);
this.addChild(pMenu, 1);
CCLabelTTF pLabel = CCLabelTTF.labelWithString("Hello World", "Arial", 24);
CCSize size = CCDirector.sharedDirector().getWinSize();
pLabel.position = new CCPoint(size.width / 2, size.height - 50);
this.addChild(pLabel, 1);
CCSprite pSprite = CCSprite.spriteWithFile("HelloWorld");
pSprite.position = new CCPoint(size.width/2, size.height/2);
this.addChild(pSprite, 0);

return true;
}
CCSprite pSprite;
public static CCScene scene()
{
CCScene scene = CCScene.node();
CCLayer layer = HelloCocos2dScene.node();
scene.addChild(layer);
return scene;
}

public static new CCLayer node()
{
HelloCocos2dScene ret = new HelloCocos2dScene();
if (ret.init())
{
return ret;
}
else
{
ret = null;
}

return ret;
}
public virtual void menuCloseCallback(CCObject pSender)
{
CCDirector.sharedDirector().end();
CCApplication.sharedApplication().Game.Exit();
}

public override void ccTouchesBegan(List<CCTouch> touches, CCEvent event_)
{

}
}


public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.graphics.IsFullScreen = false;

TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);

CCApplication application = new AppDelegate(this, graphics);
this.Components.Add(application);
}


程序运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: