我想在游戏运行时实时看到碰撞矩形,请问要怎么才能把它画出来?或者用什么别的方法让它在屏幕上显示?
如果是C++代码实现的话,可以这样
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(Ref* pSender,TouchEventType aType);
void onDraw(const kmMat4 &transform, bool transformUpdated);
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated) override;
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
Armature *armature;
CustomCommand _customCommand;
};
void HelloWorld::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
}
void HelloWorld::onDraw(const kmMat4 &transform, bool transformUpdated)
{
kmGLPushMatrix();
kmGLLoadMatrix(&transform);
armature->drawContour();
kmGLPopMatrix();
}
重写一下draw函数就好了哦


— Begin quote from ____
引用第1楼886622林于2014-05-28 11:02发表的 回 楼主(guawoo2014) 的帖子 :
如果是C++代码实现的话,可以这样
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
....... http://www.cocoachina.com/bbs/job.php?action=topost&tid=201692&pid=962463

lua 代码,是怎么去画得呢?
— End quote
你好,, 我按照你说的重写了draw函数,,怎么精灵的碰撞框没显示出来??????问题出在哪里了??谢谢
重写draw(看你的碰撞体是什么):
CCLayer::draw(); //根据碰撞体的类型,重载draw函数,下面 是画矩形
glEnable(GL_LINE_SMOOTH);
glLineWidth(1);
CCPoint vertices] = { ccp(100, 100), ccp(200, 100), ccp(200, 200), ccp(100, 200) };//矩形的4个点
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glColor4ub(0, 0, 255, 255);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4); //画矩形边框
// restore default state
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
直接用我的文件吧, gl绘制线框
– GLRect
ClassGLRect = class(“ClassGLRect”)
ClassGLRect.__index = ClassGLRect
function ClassGLRect:onEnter()
cclog(‘ClassGLRect:onEnter’)
self._rect = cc.rect(0,0,0,0)
self._color = cc.c4b(0,0,0,255)
self._glNode = gl.glNodeCreate()
self._glNode:setContentSize(cc.size(960, 640))
self._glNode:setAnchorPoint(cc.p(0, 0))
self:addChild(self._glNode)
local function primitivesDraw()
local r = self._rect
local c = self._color
cc.DrawPrimitives.drawColor4B(c.r, c.g, c.b, c.a)
cc.DrawPrimitives.drawRect(cc.p(r.x, r.y), cc.p(r.x+r.width, r.y+r.height))
end
self._glNode:registerScriptDrawHandler(primitivesDraw)
end
function ClassGLRect:setRect(x, y, width, height)
self._rect.x = x
self._rect.y = y
self._rect.width = width
self._rect.height = height
end
function ClassGLRect:setColor(r, g, b, a)
self._color.r = r
self._color.g = g
self._color.b = b
self._color.a = a
end
function ClassGLRect:isIn(touchX, touchY)
local p = self:convertToNodeSpace(cc.p(touchX, touchY))
return cc.rectContainsPoint(self._rect, p)
end
function ClassGLRect.create()
local node = createInstance(ClassGLRect, cc.Node:create())
if nil ~= node then
node:onEnter()
end
return node
end