Flash cc As3 instance checking for direction - actionscript-3

My main question here if there is a way that as3 can check for which side or direction of an instance is true? For example, lets say there's a block. I want flash to be able to detect which direction of the block the player is on. Like check if the player is standing in the direction the right side of the block is facing.

Do you mean you want to be able to tell if something is on the left or the right side?
if (block.x < player.x) {
// block on the left
} else {
// block on the right
}

Related

Cocos2d-x 3 - Disable collision between two bodies and detect when them separate each other

I'm developing a cocos2d-x game (version 3.8). My game uses chipmunk physics and it has a static body that works like an interruptor. This interruptor is enabled when another body is over it. The interruptor is disabled when bodies separate each other.
I want to:
Moving body don't collision with interruptor. It has to cross interruptor with no bounce
I want to detect when moving body separates the interruptor
My first approach was implementing onContactBegin method. I return false when those two bodies get in touch. This way the body crosses the interruptor and does not bounce.
The problem is onContactSeparate method is not called, because contact did not happen.
If I return true in onContactBegin method, onContactSeparate is called and I can detect it. The problem is the body does not cross the interruptor, it bounces.
[EDIT] More info
This is the scenario where two sprites are separated. The ball can move and interruptor is a static body. Ball could be over the interruptor.
This is the scenario where two sprites are in contact and object1 (the ball) is over the interruptor. I want to detect where two sprites separate each other.
Any help would be appreciated!
It seems to me like you are using Box2D within cocos, so I'll answer with that as my base assumption.
This is what i would do.
My interrupter would be a b2Body* with no BodyDef dimensions defined or just a 0x0 dimension def.
I would set the user data for the bodyDef to a rectangle that describes my interruption area. This way you can always have your interruption area represented, but will not collide with anything.
(Optional) If you want the interruption area to move around based on the fake body you assigned to it, you can updated it just after the step function using something like below.
world->Step(delta, 10, 10);
for (auto physicsBody = _world->GetBodyList(); physicsBody; physicsBody = physicsBody->GetNext())
{
auto userData = static_cast<Node*>(physicsBody->GetUserData());
if(userData != NULL)
{
// Set interruptor area rectangle = physicsBody->GetPosition();
}
}
To let the rest of the system know when I have left the interrupter I would store a function pointer to the function I want to call when this happens, When a object enters the interruption area I would flag it saying "I'm in the area" after that, the first update step you get when it's not in the area anymore I would fire the callback and reset the flags I used to get to that point.
I hope this helps. You are not giving a lot of context for what you want to do, an image would be helpful. Especially when it comes to looking for help with code that has a visual representation as well.
[EDIT]
Based on the behaviour you want this is the way I did this. The first thing to know is that you don't need physics collisions for the behaviour you want. You can do this by using bounding box intersection tests and use flags and callbacks to figure out the rest.
I have an object that knows about both the ball and my interrupter nodes. In the update loop of this object I check if the two intersects. I set a flag indicating "I am in the interrupter", the next frame that I am not in the interrupter and my flag is still true I call the function that I assigned with my "leaving logic" in it, and set then flag back to false.

Object changing layer when going between frames?

I have a child of an object that stays on the scene when the frame changes. So I gotoAndStop(2); and the object is still there. However, when I come back to frame one. The object is on the lowest layer, despite the fact that I originally added it using addChildAt(character, 1); I think this adds it to the first layer? Anyone know how I can fix this issue of keeping a movieclip object on the top layer despite changing frames? Thanks.
In AS3/Flash, the bottom most layer is 0. So doing addChildAt(character, 1) would make your character the second to the bottom layer. addChildAt(character, 0) would make it the very bottom/back layer.
If you want to make it the top most layer, you do any one of the following:
addChild(character); //this is the shortest amount of code
addChildAt(character, numChildren-1); //the is exactly the same as above
setChildIndex(character, numChildren-1); //this is also the same but requires the character already be present on the display list
The latter (setChildIndex) may be preferred IF your character originates on the timeline (eg not created through code). The reason being, if you change through code the parentage of something created on the timeline, it will not go away when no longer present on the timeline.
If you want a way to force something to always be on top, you can do something along these lines:
this.addEventListener(Event.ADDED, bringToTop);
function bringToTop(e:Event):void {
setChildIndex(character, numChildren-1);
}
Doing that, makes it so whenever any other object is added as a child of this, it will set the character to the very top most layer/z-index.

cocos2dx - sub layer that covers its parent partially keep capturing all touches

In my cocos2dx game, I have a CCLayer that contains another CCLayer. The sublayer just cover part of the container layer. I 'think' I achieve this through:
this->setContentSize( CCSizeMake( 100, 200 ) );
however, the sublayer always capture touches even though it is outside its size and position area... Is it common?
I can filter through the touches position by comparing it inside the ccTouch** functions, but I think it is a hack, what is the proper way to set the sublayer to properly cover just the partial area of its parent?
The only thing i can think of straight away is making this inner layer a CCNODE and also extent it with CCTouchDelegate.
Now with this, when u register with the TouchDispatcher, you make sure it doesn't Swallowtouches(the boolean value given as the last parameter)...
This way when you receive a touch ... just see if it is within the boundary of this inner layer of urs and if it is not, send let the parent class use this touch.
Hope this helps.

Controlling character movement, and keeping score while doing so, AS3

I have created a game where you have to follow the suggested arrow (going from right to left) to control the movement of the character. As you go trace the arrow from right to left, the characters position changes and he rubs his forehead. I wanted it to be very interactive, so you feel like you are controlling the character. As he rub his forehead, I want the "heat meter" to increase, and after it reaches hot, the level is completed. Is this even possible with Actionscript 3? Thank you in advance for letting me know, and if it is, how would I go about setting this up. I am very new to AS3. :/
Use gotoAndStop() to navigate to the head scratching menu using if statements (if(var thinking:Boolean == true) and to the heat meter: if(character.currentFrame == "scratchinghead") { var heatmeter:int ++; }
Then to win: if(heatmeter > 99) { gotoAndStop("win"); }
To check if the mouse is hovering, use MOUSE_ROLL_OVER.

Making a movie clip come from other side when leaving frame

So im trying to make an easy script, which will hold a video...
And i will be able to drag the video around as i want, but the catch is... when it leaves borders of the frame, i want it to continue playing on the other side, even if its only half of the video or less...
I know how to transport it, to the other side, but not the half of it or less..
function everyFrame(event:Event):void {
if (movie.x + 100 < 1)
movie.x = 400;
}
Here is an image of the effect that im trying to achieve... http://i.imgur.com/w8E4s.png
Thank you in advance
Without seeing more code, I would say you need to set clipAndEnableScrolling.
clipAndEnableScrolling="true"
Try adding that to the group (Group, HGroup, or VGroup) that you have your video in.
Edit based on me understanding now...:
When the right edge of your movie touches the right edge of the screen, you will need to create a second movie and add it to the stage at a determined x of the first movie and y of first movie. I would say something like this...
onMoveOfFirstMovie():void
{
if(firstMovie.x + firstMovie.width > movieContainer.width)
{
if(secondMovie doesnt exist)
{
//create second movie if it isnt already on stage and set it's play position to the play position of first movie
}
secondMovie.y = firstMovie.y;
secondMovie.x = firstMovie.x + firstMovie.width - movieContainer.width;
}
}
Very rough code but it should be about right.