Cocos2d 中的粒子碰撞檢測 (Collision detection with particles in Cocos2d)


問題描述

Cocos2d 中的粒子碰撞檢測 (Collision detection with particles in Cocos2d)

I am using CCParticleSystemQuad to create a particle effect. Now I would like to test for collisions with a CGRect from my Cocos2d scene. I listed another subject similar to this one and got a little closer however I still do not have the full solution so I have re‑listed with a slightly different subject title. 

I have half of the solution. I can get the position of each particle and can test for collisions, now I would like to set the positions of each when they collide. I am currently subclassing the CCParticleSystemQuad and then adding my own getter like so:

‑(tCCParticle*)getQuadParticle:(int)quadIndex
{
    return &particles[quadIndex];
}

Then in my Cocos2d scene I can get the particle and the position:

tCCParticle *particle = [emitter getQuadParticle:i];
CGPoint pos = particle‑>pos;

This works but warns that CCParticleSystemQuad may not respond to getQuadParticle. Which is a concern, but what I would like to do now is set the position from the scene in a similar fashion such as:

[emitter setParticlePos:i newPosition:newPos];

However I am not sure how to make a setter that does this that works from my scene. I don't want to do collision detion inside the particle subclass if possible. 

I started another topic of similar nature called "How to get particle position in Cocos2d (iphone)" and I was told to overide the "update" method or the "updateQuadWithParticle" method but I am unsure how to go about this exactly.

If someone could show me an example of how to do this I would be most grateful. 

‑‑‑‑‑

參考解法

方法 1:

  

This works but warns that CCParticleSystemQuad may not respond to   getQuadParticle.

For the warning, make sure your emitter is made from your subclass (and not a regular CCParticleSystemQuad), and that your getter method is declared in the interface (.h file) and defined in the implementation (.m file).

Looking at the API, I don't see a method for setParticlePos:newPosition: but there is something that looks similar: ‑(void) updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos;

I have not used it, but a glance at the source suggests it does what you need.

So maybe try  [emitter updateQuadWithParticle:particle newPosition:newPos]; 

Hope that's helpful.

(by Kangoofith)

參考文件

  1. Collision detection with particles in Cocos2d (CC BY‑SA 3.0/4.0)

#iphone #cocos2d-iphone #XCode #collision-detection #particles






相關問題

iPhone 3.0 MPMoviePlayerController 在屏幕上顯示視頻播放器的等待時間長有問題嗎? (Problem with iPhone 3.0 MPMoviePlayerController long wait time to display the video player on screen?)

將 NavigationBar 背景更改為圖像 (Changing the NavigationBar background to image)

如何將以下行添加到我的 plist 文件中? (How can I add the following lines to my plist file?)

iPhone SDK:檢查 UITableView 中每個對象的 ID 號並相應地更改徽章號 (iPhone SDK: Checking the ID number for each object in a UITableView and change the badge number accordingly)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

嵌套 XML 示例? (Nested XML Example?)

提交應用程序 - 添加本地化,但我的語言未列在組合框中? (Submitting app - Add Localizations, but my language is not listed in the combo box?)

如何從移動瀏覽器直接訪問移動設備的硬件功能 (How to gain direct access to the hardware capabilities of mobile devices from mobile browsers)

Cocos2d 中的粒子碰撞檢測 (Collision detection with particles in Cocos2d)

捕捉返回按鈕導航事件 (Catching back button navigation event)

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

帶有 UITableView 的 UIPopoverController (UIPopoverController with UITableView)







留言討論