Boolean question in Cocos2d - iphone

I am making a game in Cocos2d. I have enemies in the game. I want them to shoot at the character. Currently, I have a boolean that says
buffDude.shoot = YES
when it is done moving. And in the Enemy class, I want it to detect if the boolean is YES or NO, and shoot if it is YES. And, while we're on that note, if I declare
buffDude.shoot = YES
in the
+(id)enemy
method, it will create a bullet at the bottom of the enemy sprite, but the bullet will not move. I know that it is because it didn't add the bullet to the Layer, it added it to the Enemy, but I don't know how to add it to the layer. Please Help! This is really driving me crazy, and help would be appreciated.
EDIT:
Okay, to get it to work, I just created a different layer and added the enemies and their bullets to that. Thanks for the help!

I am not really sure what you are trying to do. But I can only assume that you are adding a bullet sprite. And if you are adding a bullet sprite in you Enemy Class like: [self addChild:bulletSprite];, the Enemy class will own that bullet. Instead, if you would like the bullet to be visible in your game scene, you can add the same code in HelloWorldLayer.m instead.
But as I said, I'm not really sure what your problem is.

Related

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

Setting up a sliding character in the right way

I would like to try and make a game similar to Altos Adventure. I am having trouble starting.
How to set up the terrain so that my character doesnt get stuck?
I simply drew a test sprite for the ground in photoshop. What collider2d should I use?
I use polygon collider and reduce friction in the material. But the character gets stuck, hits small invisible bumbs and it feels awful. The worst part is connecting 2 ground sprites together! The point where they connect is always messy.
I have a question for the future as well. How would I add the endless part of the game?
Would having many "pieces" set up and just spawning them as the player rides down work?
I havent written any code yet as the problem is simply in the physics in my opinion.
Thanks to anyone who takes the time and tries to help!
To move your player use rigid body.AddForce(Vector3 direction, ForceMode forceMode); then create a Physics Material with friction = 0 and put it in your player's collider.

Unity- Colliding Passing Through

I made airplane shooter in Unity. First I make player and I already make all the code. And it works well. And then now I want to change the player object. I had already put the script and give same name and tag as the used player that I make. And the colliding is a mess. I can shoot enemies down. But the enemies can't shoot or hit me. The enemies bullet and the enemies passing through my player object.
I don't know why. Please help me to find the problem. Just tell me the possibilities of this problem cause.
Here is the screenshot of my new object component
Check if your Collider has the "Is Trigger" checked, that may be causing the error.

swift usesPreciseCollisionDetection does not work?

i have made an app where you need to shoot on an enemy but sometimes the bullet goes through the enemy. swift sprite kit game when I shoot on a enemy sometimes the bullet goes trough the enemy, how can I fix this?
so i added usesPreciseCollisionDetection = true.
but the bullet sometimes still goes trough the enemy. so my question was did i do something wrong or is usesPreciseCollisionDetection not working?
thank you for reading my question I hope someone can help me.
You are using moveTo to move your bullets, Physics Engine does not work with moveTo the way you want it to, usesPreciseCollisionDetection will not do anything
As far as your Physics World is concerned, your bullet is stationary. You are just playing God and performing some kind of molecular transportation to move items.
You need to use functions like applyImpulse or even just give the physicsBody a velocity if you want this to work correctly
I had similar problem that is collision was sometimes not detected even when usesPreciseCollisionDetection was set to true and both SKPhysicsBody were rectangles. In my case usesPreciseCollisionDetection for some reason started working much better when I changed one of the SKPhysicsBody to circle instead of rectangle. Try changing your bullet physics body to circle, it seems that collision detection algorithm with circles is more precise so that might help you.

Detect collision with enemies head with cocos2d

I want to detect a jump on an enemies head (like in super mario). How can I do this?
If you didn't understand what I mean just say something :)
I dont need to use cocos2d but it would be better with that :)
I'm not familiar with cocos2d so I can't help with specifics but:
If you can determine the player collided with an NPC, just check if the bottom of the player is what collided with the top of the NPC. If it did, he must have landed on his head.
To be extra sure, you could also check if the player's velocity states that he's moving down but that's probably not necessary.