the collision not working i cant understand why, i put collision movieclips in the object and it doesnt seem to recognise one of the but does with the other, sorry for the confusing way of stating the problem if you play the game you will understand. im open to changing the way collision works too as long as it works ill be super happy
I'll try to explain. When You click "Down" or "Up" hero (box_MC) collide with both doors "Top_Door" and "Bottom_Door". Inside "bang" function at first checking collision with "Bottom_Door", so, hero always go down (.y += 100) and second condition (Top_Door) never will be true. How to fix this? Add variable var lastAction:String;. This variable will store last action: "up" or "down". Inside "down_MC_P" function initialize this variable by "down". Inside "up_MC_P" — "up". Next, replace the first condition to this if (box_MC.hitTestObject(cycle[i].Bottom_Door) && lastAction == "down") and second: if(box_MC.hitTestObject(cycle[i].Top_Door) && lastAction == "up"). That's all.
Related
I'm a beginner of using cocos2d-x.
My problem is I dun know how to keep the CCSprite to another scenes.
The details of my case:
I've made a class"Scene01"includes 5 characters CCSprite with attributes, each of them class name like C1,C2...C5.
I've made a "Draw" button at class"Scene02"to draw out 1 of them randomly. I put this action at "CCTouchesBegan"...the character draw setting as below:
if (probability >0 && probability <=20) {result = C1::create();}
else if (probability >20 && probability <=40){result = C2::create();}
...until C5::create();
I use "this->addChild(result);" display on "Scene02" at "CCTouchesBegan".
But I don't know how to keep the generated "result(CCSprite)" to the new scene class"Scene03". Is there any better way(s) to simplify my case or any method(s) can help me to complete it?
You could try the following: retain the Sprite, remove it from Scene02 (keeping it on the heap) and then add it to the Scene03.
//(Scene02)
result->retain();
result->removeFromParent();
..
//(Scene03)
this->addChild(result);
result->release();
I am new to flash and want to make it so when my character hits an object, they won't go through it, but still maintains control after they have hit it. I want it to be a solid object from all 4 points (top, left, right, bottom) of the object. Here is what I have been experimenting with...
function hitsTheObject(e:Event)
{
if (myCharacter.hitTestObject(Ball_mc))
{
gravity = 0
hitObject = true
}
if (dIsDown == true && hitObject == true)
myCharacter.x -=10
}
The first if statement works, though the second one turns off the dIsDown button I have coded. Any thoughts?
Edit: Basically I want the character to hit an object and for it to block the character, as if it was a wall.
Lewis, you're best bet is point collision. Here's some links to help you think it through and start off: http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/ and http://www.anotherearlymorning.com/2009/07/pixel-perfect-collision-detection-in-actionscript-3/
Check out my collision engine, it supports continuous/bullet collisions: https://github.com/Murplyx/AAE---Axis-Aligned-Engine
Okay, so let's say I have "ball" MovieClips that collide against a "wall" MovieClip.
if ( !ball.hitTestObject(wall) ) {
// If they didn't find any obstacle keep falling.
ball.y++;
}
Then I remove the wall, and the balls keep colliding against a wall that I cannot see. No ball moves, not the ones that were there, neither the new ones.
case Keyboard.K: removeChild(wall); break;
What am I missing or doing wrong?
Thanks in advance.
removeChild only removes the instance from the display list, display list has nothing to do with game logic actually... you have to either disable your movieclip with a boolean (if you later want to use it then you can enable it) or null the reference, but beware of nulling your reference, because if you try to access it after nulling, you get an error, so you might want to check that if your wall is whether null or not before checking your collision.
if(wall!=null){
if ( !ball.hitTestObject(wall) ) {
// If they didn't find any obstacle keep falling.
ball.y++;
}
}
...
case Keyboard.K:
removeChild(wall);
wall = null;
break;
I'm trying to check if an object is visibly "blocked" by another object at a certain xy spot. The normal hitTest method only states if two objects overlap or not, not which is visibly in front. Is there any way of achieving that, by having the xy-coordinates and two objects? So for example: 2 objects are on stage. Nr. 1 is in front and visibly covers Nr. 2 ... so an xy-hitTest with both objects should only return "true" for object nr. 1.
Thanks in advance :-)
The following function will use hitTestObject and the child indices of the objects to determine if obj1 is "in front of" obj2.
function isInFrontOf(obj1:DisplayObject, obj2:DisplayObject):Boolean{
return obj1.hitTestObject(obj2) && (obj1.parent.getChildIndex(obj1) > obj2.parent.getChildIndex(obj2));
}
Important note: This will only work if both objects have the same parent. You absolutely can rework the function to allow for different parents, but I'll leave that up to you.
Confusing title, my bad.
Basically, I have a list of names. Looping through, I add a MovieClip, Set 2 properties to it, the name, and an ID. The MovieClip is at the same time made to function as a button and I add 4 listeners, mouse up, over, down, or out. I do this with every name. The function each one is set to is the same.
EX: enemyButton[i].addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
The enemyID turns up "not valid property," time to time when I click, it doesn't crash at all, but sometimes I have to hit the button a few times.
I have narrowed the problem down to having to be caused by the listeners.
The function as simple as:
EX: function mouseUpHandler(e:MouseEvent):void { enemySelected(e.target.enemyID); }
My question is, is too many listeners likely to be the problem? and how can I reduce them?
Here's a snippet of the loop:
var C:Class = Class(getDefinitionByName(enemies[i]));
var c:* = new C();
c.gotoAndStop(1);
enemyButton[i].enemyID = i;
c.name = "select" + i;
c.enemyID = i;
trace(c.enemyID);
enemyButton[i].addChild(c);
enemyScroll.addChild(enemyButton[i]);
enemyButton[i].enemyName.text = info[i][Const.NAME];
enemyButton[i].setChildIndex(enemyButton[i].getChildByName("enemyName"), enemyButton[i].numChildren-1);
Thanks.
If enemyButton is a MovieClip (created via attachMovie, maybe) and not strongly typed as a EnemyButton class, then the ID property becomes dynamic. In this situation, if your list of names contains incorrect data (missing ID field, maybe), then the ID property will remain undefined on some instances of the MovieClip.
You can check the list of data used to generate movie clips. You can run into the same error if you have blank lines in your data.
This has nothing to do with event listeners.
So you just want to generate a bunch of buttons with unique properties and know what button was clicked last. Generally it is very bad idea to implement button logic outside button object. Why? Because you work with object oriented language. Good news is that you work with as3 and it treats functions as objects, so you can assign function to var like this:
var callback:Function = function(name:String, enemyId:int){ /*do something*/ }
And.. you can pass function as a parameter to another function
function setCalback(func:Function){}
button.setCallback(callback);
So, what you really need is to create your own button class, add listeners inside it, add handlers(static handlers will reduce memory usage) and pass callback function to it, that will be called when user clicks button.
Don't mean to spam this much but this was easily fixed, though the responses might have been a better method.
I just had to change target to the currentTarget, that then allowed clicking anywhere on the "button" to work. Whereas before the target varied from childs added to it.
So, solved.
Thanks for the help.