I'm making a Pong game in Actionscript 3 and I'm pretty much done it, however I need to create a feature where if the paddle passes the top of the screen it goes back to the bottom, and if it passes through the bottom it appears at the top. How would I create something like this?
Y axis is for up/down so use paddle.y to check the position...
Some logic like this :
if ( paddle.y <= (-paddle.height) )
{ paddle.y = ( stage.stageHeight - paddle.height ); }
Just an example you can test and refine. This assumes your paddle is on stage directly. If the paddle was instead added to some container (Sprite or MovieClip) then instead you use:
{ paddle.y = ( containerMC.height - paddle.height ); }
If you want an effect where, for example, the top-half of your paddle is above screen then there is a half-paddle at the bottom... for that you just use two instances (copies) of the paddle Sprite.
Related
I am creating a game like bounce ball using cocos2d-x and box2d. I have different objects like rectangle, square etc.. I am able to detect collision, but i want to detect collision only on top of the objects. What exactly i want is, when the ball is on the top of the object, then only i want to jump the ball.
But, when the ball is collide on remaining side(bottom or left or right) i don't want to jump the ball.
In touchbegan, i am using the following code to the bounce ball. So every touch it is jumping when it collide with remaining side.
if(_ball->boundingBox().intersectsRect(rect->boundingBox()))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
Any advice?
Following steps can solve your problem-
1) CCRect projectileRect = CCRect(float x, float y, float width, float height);
if(_ball->boundingBox().intersectsRect(projectileRect))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}
2) - Make body of an object and then check their collision.
I got the solution for my question from the below link.
http://www.raywenderlich.com/28606/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-2
I am trying to make an actor follow the player's finger (long touch). I'm positive I have the math right, but the actor fails to move exactly to where the player touched.
Here is an illustration of my problem:
When the touch is near the top, the actor goes beyond the visible scene at the top.
When the touch is near the bottom, the actor goes out of the visible scene at the bottom.
Same goes for the left and right.
When the touch is performed in the middle of the scene the actor moves perfectly to the touch. In short, the further the touch is away from the middle the more pronounced the distance between the actor and the touch is. In other words; the closer the touch is to the middle, the closer the actor moves towards the touch.
Please note that when the touch was near the bottom or the top the distance between the touch and the actor was more pronounced then when the touch was on the right or the left; as the top/bottom are further from the mid point.
Here is the code used to follow the actor towards the touch:
Lang: Lua
Lib: Cocosd2-x 3.1
local velocity = 1.4
local x, y = self.sprite:getPosition()
-- self.dest[X/Y] are cached coordinates to where the actor should move next.
local angle = math.atan2(touch.y - y, touch.x - x)
local deltaX = velocity * math.cos(angle)
local deltaY = velocity * math.sin(angle)
local newX = x + deltaX
local newY = y + deltaY
self.sprite:setPositionX(newX)
self.sprite:setPositionY(newY)
Things I've tried:
Changed the scale of background layer and sprites. No change
Changed the algorithm used to compute the angle. No change.
Created a red dot and set its position to the exact touch x/y to determine if there was some weird transformation issue when determining the actor's point. The red dot was always perfectly under the touch.
Discovered the issue. When I created the Actor sprite I set its z-index to 100. When I uncommented out the call that set the z-index, everything worked perfectly. In my situation, this particular sprite must always be above all other sprites. What I did to fix the issue is set the z-index much lower than what I had originally set it to; which ended up being 15.
sprite:setPositionZ(15)
From my observation it appears that the sprite is having some type of scale applied to its position the larger the z-index is of the sprite.
Update 1
Using :setPositionZ(int) will unnecessarily scale your sprite bigger in some cases. I now use :setGlobalZOrder(int) with much better success:
sprite:setGlobalZOrder(15)
I am new to AS3 and I wanted to know how to make a frame Boundary in flash, I am making a platformer game. My code Logic is like :
Whenever I move with ArrowKeys Move Background invert, so if I click Left key the background will move Left. so this is not really what I want as my Player (moving avatar) will always be stick to the middle of the stage. How to make a New boundary so whenever Player go near Right end of Stage ... Background should go left as much x moved outside boundary of player.
more illustration :
http://i.imgur.com/HOw6vHI.png
if(player.x > rightBound){
map.x += rightBound - player.x;
player.x = rightBound;
}
Repeat for the other 3 sides.
i've been making a twist on the labyrinth game and i've got my ball to move with physics but im struggling with getting it to hit the walls around it. its currently a movie clip with black walls, and ive used this code to try and stop it:
if (character.hitTestObject(walls)){
character.x = //something
character.y = //something
}
all this does is when it hits any part of the movie clip, (even the blank spaces) it moves my character,
is there any sort of code i can use to maybe detect hitting a certain colour?
One way you could do this, is to use hitTestPoint() method to test if any of the corners have hit your wall.
hitTestPoint() tests only a single location to see if that point collides with an object. This is how you could test the top left corner of your character to see if it's touching the wall :
// I am assuming that x,y is the top left corner of your character
if (wall.hitPointTest(character.x, character.y, true))
{
// top left collided with wall
{
So you could do the same for all corners, or if you want, you can determine any collision points you want to check for the character.
Depending on your level of precision, this method might work just fine for your needs. But if you want pixel perfect collision, you can check out this link :
http://www.freeactionscript.com/2011/08/as3-pixel-perfect-collision-detection/
I am looking for direction to this old UFC effect - http://84.ufc.com/ that appears on the main page. It is movieclips rotating on different axis based on the mouse position. So far I have found this script:
stage.addEventListener(MouseEvent.MOUSE_MOVE,EnterFrame);
function EnterFrame(e:Event)
{
mc.rotation = (180*Math.atan2(mouseY-mc.y,mouseX-mc.x))/Math.PI + 90;
}
But this only rotates on x and y. What's a way to approach this effect? Please any suggestions. I have searched this for months.
If you are using Flash CS4+ and targeting Flash Player 10+, you can use the 3D DisplayObject APIs (aka "postcards in space") to achieve this effect! All DisplayObjects will have x, y, z, rotationX, rotationY, and rotationZ properties that you can tweak.
Create a movieclip and place it on the stage. The origin--the crosshair that appears when the clip is selected--should be in the middle in the stage. Give the movieclip an instance name of clip.
Double-click the movieclip, and place other movieclips inside it. Use the 3D Rotation and Translation tools to orient these clips in 3D inside your parent clip. You can find the 3D tools in your toolbar -- it has an egg-like icon, or press the W or G keys on your keyboard.
Now, here's some simple code that will tweak the orientation of that parent clip based on the mouse position:
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
clip.rotationX = (stage.mouseY - stage.stageHeight/2) * 0.1;
clip.rotationY = (stage.mouseX - stage.stageWidth/2) * 0.1;
}
You can play around with this to come up with many other effects. Note that you can only do simple 3D effects with these properties, however. You can't do full 3D rotation, because the clips won't be sorted from back to front. For more complex effects, you'll want to use a framework like Papervision3D or Five3D.
i just found out...
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
anim.rotationX += ((stage.mouseY - stage.stageHeight/2)-anim.rotationX*3) * 0.05;
anim.rotationY += ((stage.mouseX - stage.stageWidth/2)-anim.rotationY*5) * 0.05;
}