gpt4 book ai didi

c++ - 通过 Box2d 中的 NSNotificationCenter 发送 C++ 类对象。错误 : No viable conversion from 'MyContact' to 'id'

转载 作者:行者123 更新时间:2023-11-28 07:51:55 25 4
gpt4 key购买 nike

我尝试在 box2d *cocos2d* 中实现Contact Listener。我想将 NSNotificationCenter 集成到 PostSolve 函数中,但是当我尝试指向 object: 参数时,我遇到了错误:

No viable conversion from 'MyContact' to 'id'

如何通过 NSNotificationCenter 发送 MyContact 对象?

MyContactListener 类

 //
// MyContactListener.m
// Box2DPong
//
// Created by Ray Wenderlich on 2/18/10.
// Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "MyContactListener.h"
#import "ConfigMacro.h"

MyContactListener::MyContactListener() : _contacts(),_contactsObjects(){}

MyContactListener::~MyContactListener() {}

void MyContactListener::BeginContact(b2Contact* contact) {

b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();

if(bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
}
}

void MyContactListener::EndContact(b2Contact* contact) {

MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::vector<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {}

void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {

b2Body *bodyA = contact->GetFixtureA()->GetBody();
b2Body *bodyB = contact->GetFixtureB()->GetBody();

if(bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
int32 count = contact->GetManifold()->pointCount;
float32 maxImpulse = 0.0f;
b2Vec2 collision;

for (int32 i = 0; i < count; ++i) {
maxImpulse = b2Max(maxImpulse, impulse->normalImpulses[i]);
if (i == 0) {
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
collision = worldManifold.points[0];
}
}
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB(), maxImpulse, collision };

[[NSNotificationCenter defaultCenter] postNotificationName:kReceivedImpulse object:myContact userInfo:nil];

}
}

最佳答案

object: 参数应该是一个指向 Objective-C 对象的指针 - 您正在尝试传递看起来像结构的东西。

关于c++ - 通过 Box2d 中的 NSNotificationCenter 发送 C++ 类对象。错误 : No viable conversion from 'MyContact' to 'id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com