Libgdx move an actor on stage - libgdx

moveSkill.addListener(new ActorGestureListener() {
#Override
public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) {
super.pan(event, x, y, deltaX, deltaY);
Vector2 v = new Vector2(Gdx.input.getX(), Gdx.input.getY());
GameActors.skillTable.setPosition(v.x, v.y);
}
});
Hi there, I'm trying to achieve a 2D element move around, like you would do with a Window or Linux element, to drag it and drop it on somewhere, without MTA, without drag and drop class, and to be honnest, this actor is part of a hud,
To be clear I do want to move around let's say for example an inventory in the screen to gain place and display something else, like Player equipment and Inventory on the same time, don't know if my explanation is clear so lets get an example:
See there is two "Sac" Positions, in this game you only have to click where "Sac" is written and you can drag it aroung, and release it to a new position.
Another question is, why each time I click on my actor to move it around, it kinda jumps?
Here is actually what I got:
https://youtu.be/mjngGwUy-Pw
Any help would be appreciated

Related

detect collision only on top of the object in box2d & cocos2dx

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

Overriding the x getter in Actionscript 3.0

I have a class called Element. It is basically my ultra-movieclip, and nearly all classes in the game subclass it. Element extends Movieclip
The code in the game is built around the idea that all Movieclips are registered at the top-left corner. Register it somewhere else, and everything starts to fall apart.
The problem is, sometimes the in the animation and such, the top-left corner moves, but the registration point doesn't, and you end up with a registration point somewhere in the middle of the Movieclip. This causes problems.
So, how do I fix this problem? We can find the true top-left corner of a Movieclip using getBounds (this is the documentation for it), but using getBounds over and over in code would get kinda tedious.
My idea is to override the x getter in the Element class. That way, we can always know where the Element's true top-left corner is, without having to write a bunch of complicated code every time!
And for the sake of argument, let's assume that just making a new getter like "trueX" will open a trapdoor beneath my feet and plunge me into a cage of starving gophers.
I cannot figure out how to override the x getter! How can we do this? Have I missed an incredibly simple solution to the problem that has nothing to do with overriding?
override public function get x():Number {
//I have no idea what I'm doing!
}
This should be all you need to do: (if I understand your question right)
override public function get x():Number {
return super.x + getBounds(this).x;
}

libGDX zoom implementation isn't working right

I would like to achieve a zoom effect that is similar to "Clash of Clans". I'm currently implementing zoom in a way that pretty much all the tutorials online do. There are of course a my camera specific constants to make it zoom at good speeds for my camera.The code DOES WORK.
#Override
public boolean zoom(float initialDistance, float distance) {
float zoomAmount = -1*(distance - initialDistance)/10000;
cameraHelper.addZoom(zoomAmount);
return true;
}
The camera helper class does this simple stuff:
public void addZoom(float amount) {
setZoom(zoom + amount);
}
public void apply(OrthographicCamera camera) {
camera.zoom = zoom;
camera.update();
}
So this does work. When I fling two fingers in one continuous motion and then stop moving the fingers on the screen, let's say I zoom in with zoomAmount = x, zoom gets called once and the zoom is done.
Problem:
However, if I then wiggle one, or both, of the fingers just a tiny bit, zoom gets called again with that same value for zoomAmount = x. So by wiggling a finger, it will simply keep adding to zoom and keep zooming in or out (depending on the current state of zoomAmount).
Question:
How do I make libgdx or java remember my original camera.zoom much like initialDistance in the zoom() method. OR, is there any other trick that I can do to have a responsive and smooth zoom (without removing fingers from the screen) much like the game "Clash of Clans".
Extra info:
I have tried pinch, same effect. I have thought about having a base zoom value and then some kind of percentage multiplier, however because I have to update the camera by setting its zoom (camera.zoom = zoom;) I can't think of a clever trick with adding or subtracting any values that will not be effected by zoom() being constantly called. Any thoughts anyone?

how to create some dynamic sprites falling from top to bottom

I am making a game in cocos2dx(c++).Here i have to make dynamic sprites that are falling top to down and on touch i have to kill them.I mean the sprites are of insects and they will be killed on touch
The problem is that I am unable to get the idea on how to implement them as on increasing the level the insects will be falling more and with more speed.
I have made for one insect.But don't know how to handle for multiple insects
Suppose, your 1st Sprite is coming from y height and it fell to -y/2 with a velocity of 10.
Then, you have to make several sprites with differnt velocities and different positions of x.
You can move a sprite in cocos2dx with the help CCMoveTo()
You can use this custome method to move sprite
void GameLayer::_mov(CCSprite *cp, float x, float y)
{
cp->setPositionX(cp->getPositionX()-x);
cp->setPositionY(cp->getPositionY()-y);
}
In this method we passing sprite which we have to move and x is how much change in sprite position and y is how much change in sprite position. In this I subtract x and y from the current position of sprite but you can add also according to your requirement.
You have to call this method in your update method.

Rotating a Model in Away3D

Here's what i want to do:
I want to load a model (most likely .3ds) into my .swf and be able to rotate it with the mouse.
This works fine at first glance, but there's problem, the rotations 'switch' over. Or to say it differently:
Imagine we have a model in the shape of a pipe.
If we drag the mouse to the right, the model rotates along its X-Axis to the left, mouse to the left, X-Axis rotation to the right, moving the mouse up, Y-Axis rotation downward, mouse down, Y-Axis rotation upward.
Now, lets say we turn the pipe to the right or left, until we face the (former) 'backside' of the pipe, and then we move the mouse down. The model will rotate downward instead of upward.
I hope you understand what i mean with this. I've been looking around for a good while now and never found a satisfying solution. There was talk about quaternions, but i can't grasp them.
Another suggestion i read somewhere is the following:
create a Matrix3D object, apply rotation on it, then multiply it with the desired Matrix3D of my 3d-Model.
I tried to do it, but the result stays the same, the directions of rotation switches depending on what side i'm facing.
private function update(e:Event):void
{
xCalc = (0.3*(stage.mouseX - lastMouseX));
yCalc = (0.3*(stage.mouseY - lastMouseY));
if(move)
{
var objTransform:Matrix3D = new Matrix3D();
objTransform.prependRotation(xCalc, Vector3D.Y_Axis, objC.pivotPoint);
objTransform.prependRotation(yCalc, Vector3D.X_Axis, objC.pivotPoint);
mesh.transform = multiply3DMatrices(mesh.transform, objTransform);
}
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
view.render();
}
multiply3DMatrices simply multiplies two 4x4 Matrices together.
objC is the ObjectContainer3D that holds my model. For some reason i cannot rotate it properly, unless i manipulate the rotationX/Y/Z properties.
mesh is the mesh inside of the Model (which is very simple, a single mesh).
Also, i'm aware that i could try another Framework for this (like papervision) but this project requires me to use Away3D.
Solved it by myself, the problem was that i created a new Matrix3D Object every time. The fixed code looks like this:
private function update(e:Event):void
{
...
if(move)
{
var objTransform:Matrix3D = mesh.transform;
objTransform.appendRotation(xCalc, Vector3D.Y_Axis, objC.pivotPoint);
objTransform.appendRotation(yCalc, Vector3D.X_Axis, objC.pivotPoint);
mesh.transform = objTransform;
}
...
}
And yes, the user bwroga was actually right, i should've used appendRotation instead of prependRotation, as well.