MovieClip within MovieClips frame change on hitTest - actionscript

I have a car that drives around a map named "car" that's within a movieclip named "map".
I have a character named "player" who has 2 frames, "norm" which contains another movieclip named "playerNorm" which is the normal state of the character. In "dead", the character is dead. I want it so that when "player" collides with "car", "player" will show it's "dead" state.
stop();
onEnterFrame = function ()
{
if (_root.playerNorm.hitTest(_root.car))
{
this.gotoAndStop("dead");
}
};

Although you are not clear what your problem is, I assume it to be 'The above snippet is not working' the way you want. If so, then you need to replace :
this.gotoAndStop("dead");
with
_root.playerNorm.gotoAndStop("dead");

Related

Swift SpriteKit Multiple Nodes in Scene with same Condition

I have multiple spritenodes in the Scene with the same Name. If I want to edit them all in the .swift file, just one reacts to the conditions and all of the other Nodes stays like in the scene.
For example if I want to let them move, just one moves... How can I fix it? All nodes are completely the same except size and position...
Thanks!
Just because nodes share the same name doesn't mean they share the same properties. This is the same with people. If I meet 2 people named John, and I shaved the first Johns head, it doesn't mean the second John is bald.
What you need to do is enumerate through all of the children with the same name.
In Sprite Kit, we have a method for that, called enumerateChildNodes and you would use it like this:
node.enumerateChildNodes(withName:"John")
{
//[unowned self] Include this if you are using self to avoid a retain cycle
node,stop in
node.head = shaved
}

box2d - count the objects of some kind "grouped" together

In a physics-based CoronaSDK game I have a number of color balls. They all collide with each other. However I'm interested in those balls grouped together by their color.
So, each of the balls has a "color" property: ball.color = "red" for example.
In the listener for collisions I check if ball collided with the one with the same color:
local function ballCollision ( self, event )
local otherBall = event.other
if ( otherBall.color == self.color ) then
-- do some stuff here
if ( event.phase == "began" ) then
-- add the ball to the group
else
-- remove the ball from the group
end
end
end
ball.collision = ballCollision
ball:addEventListener ( "collision" )
Now, I was thinking about creating a global, module-wide, table of "groups", where I could keep a table of grouped balls. With each began phase of the collision I could add a ball colliding to the group the other collider belongs.
With each ended phase I could remove it from a group.
But this presents some (quite heavy, I think) calculations, for when the larger group is split to several smaller groups by one ball leaving it...
Is there some better solution to perform this? Like - getting a list of "chained" objects, or at least getting a list of colliders for each physics object?
Unfortunately I don't think you can avoid some kind of graph-traversal check when two balls separate, to find out whether that separation caused the group to become two groups, or whether the group remains intact because other parts of it still touching.
About getting a list of chained objects, the original Box2D (C++) has a function b2Body::GetContactList() which gives you a list of all other bodies currently in contact. You could use that for the graph-traversal check instead of keeping track of connectivity information yourself. I don't know whether Corona exposes that for you though...
If you are able to use that, keep in mind that it would give you all bodies in contact, so you would need to check again that the colors are matching as you traversed them. You should also check that IsTouching() is true for contacts because in Box2D the existence of a contact just means that the AABBs of two fixtures are overlapping.
If that function is not available, I guess you will need to maintain a connectivity graph yourself using the begin/end contact events.
I am assuming some kind of match-x or PuyoPuyo type of game here, in which case I doubt the number of balls involved would cause the processing to be too heavy. If you have hundreds of balls though, yeah, that could get slow.

Making two object collide but moving like kinematic objects

I'm working on a "Words in a pic" clone and I have different images representing each letters and empty boxes where the letters should be put in to.
When I drag the letters I want them to be dragged like when it is a static body i.e. just up, down, left and right (no turning or spinning) and when the item is within the box it should stay within that box, otherwise it should go back to it's original position.
The thing is that static objects can't collide with another static object nor can a kinematic object collide with another object so I need to use Dynamic if I have understood it correctly?
However how do I do so when the drag event is activated the body, the letter image, moves like a static or kinematic body (only up, down, left and right) but also detects collision between a letter image and a empty box image?
Thanks for helping me with this, I have not been able to find any information on how to solve this problem!
This was easier than I though, you set the items as "dynamic" and then object.isSensor = true, to make it not rotate object.isFixedRotation = true and also deactivate the gravity through object.gravityScale = 0

adobe flash cs5 how to move the container & childrens

So how can I craete a container for a tank(without code as3) to move the tank(using as3 like tank.x += tankSpeed) itself and at the same time all his parts(wheels, turrel, other stuff), because i don't want to move all the parts independently in each frame, for example rotating the turrel and at the same time move the container of the tank, I didn't found in adobe flash cs5 how to add shapes(rectangles, circles...) to some main container and give it some object name and then get something like this in the code(as3):
container //get the container itself
container.child1
container.child2
if it possible of course
Create a new MovieClip in CS5, put it on the stage, and give it the instance name tank. Inside that MovieClip draw your tank but put each of the moving parts into MovieClips of their own each with a unique instance name, say wheel_1, wheel_2, turret, other_thing.
Now you can manipulate the parts of your tank independently from code on the main timeline like this:
tank.wheel_1.play();
tank.turret.stop();
tank.wheel_2.gotoAndPlay('reverse');
A simple routine to move the tank would look something like this (untested):
var speed = 10;
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
// Execute on each frame
function enterFrameHandler(event:Event):void
{
tank.x += speed;
}

Corona/Box2D detect collision with non-moving static objects

For posting reasons here's a simple version of what I'm trying to do.
On the screen I have a simple circle object that is static and doesn't move. The user can then drag and drop a straight line. If the line passes through that circle I'm hoping for the collision event to be triggered.
It appears that unless one of the objects are moving the collision is never detected. Can I detect the collision when the line is drawn?
Collision Event
function onHit(e)
print("hit");
end
Runtime:addEventListener("collision", onHit)
Touch Event
local startX = 0;
local startY = 0;
local endX = 0;
local endY = 0;
function onTouch(e)
if(e.phase == "began") then
startX = e.x
startY = e.y
elseif(e.phase == "moved") then
endX = e.x
endY = e.y
elseif(e.phase == "ended") then
local line = display.newLine(startX, startY, endX, endY)
line:setColor(100, 100, 100)
line.width = 2
physics.addBody(line, "static", { })
end
end
Runtime:addEventListener("touch", onTouch)
Create circle
local c = display.newCircle(50, 50, 24)
physics.addBody(c, "static", { radius = 24 })
This page from the Corona SDK docs describes the bodyType property about halfway down the page. When describing "static" bodies, it says (my emphasis):
static bodies don't move, and don't interact with each other;
examples of static objects would include the ground, or the walls of a
pinball machine.
That means that one of the objects has to be something other than static.
Here's an idea, although I haven't tried it myself: (See update below.) Make the line dynamic when you first create it. Set it to static a few milliseconds later using a timer.performWithDelay function. If a collision event occurs in the meantime, you will know that you've got an overlap, and can set the bodyType back to static immediately. If you don't get a collision event, the bodyType will still be dynamic in the delayed routine, and you'll know you didn't have an overlap. In this case, you'll still need to set the line to static in the delayed routine.
UPDATE: Tested this, using your code as the starting point
I changed the collision event to always set both objects' bodyType to static:
function onHit(e)
print("hit")
e.object1.bodyType = "static"
e.object2.bodyType = "static"
end
Then I changed the addBody call for the line to add it as a dynamic body, with new code to setup a timer.PerformWithDelay function to check after a short time:
physics.addBody(line, "dynamic", { })
timer.performWithDelay(10,
function()
if line.bodyType == "dynamic" then
print ("NO OVERLAP")
line.bodyType = "static"
end
end)
The results were, unfortunately, mixed. It works most of the time, perhaps 95%, but fails occasionally when drawing a line that starts outside the circle and ends inside, which should be an overlap, but is sometimes reported as no overlap. I wasn't able to figure out why this was happening. I'm posting this anyway, hoping that it gets you going, and also thinking that someone may be able to figure out the inconsistent behavior and educate us both.
Failing that, you could add an additional check for the "no overlap" case to check if either endpoint of the line is closer than the radius of the circle away from the center. That would be make things work, but I suppose it misses the whole point of letting the physics engine to the work.
Anyway, good luck!
Perform a raycast when you release your mouse press. This way you can keep both objects static, and know that they intersect via the raycast callback.
(I know that this is an old post, but it was the first hit on my Google search and is incorrect afaic)
Set the body type as kinematic, set it as a sensor, and update its position to the entity it's tied to every frame. Unlike static, kinematic can interact with static.

Resources