Andengine - adding sprites avoiding collisions with existing - android

I'm creating sprites and adding it to scene in loop in random places. I just want to check if newly created Sprite will cause collision with one of existing ones. Is there any simple way to check it?

When you create a new sprite, add user data to it:
sprite.setUserData("sprite");
And then, after you created a positioned the sprite, before you add it iterate over the existing sprites:
int count = scene.getChildCount();
for(int i = 0; i < count; i++) {
IEntity entity = scene.getChild(i);
if(entity instanceof Sprite) {
if(entity.getUserData().equals("sprite"))
if(((Sprite)entity).collidesWith(newSprite))
//Don't add the new sprite.
}
The user data can be anything you want, it doesn't have to be a string.

Related

Array creation - Zygote error

I am relatively new to Android and i want to create an array that increases in size and i declared an array like this:
private Sprite something[]; //sprite is my custom class that does sprites
And i want to add elements like this, but it gives me error...
something[i++] = new Sprite(spriteSheet,numRows,numColumns,true,x,y);
//i is number of elements and Sprite(...) is the constructor
Can someone tell me what i am doing wrong here, and why i get the zygote error and a null pointer exception?
Thanks!
Java doesn't really work like Javascript does. Arrays have fixed size, as per something = new Sprite[10]; where the element indices of something are 0 to 9.
The functionality you want can however be achieved using List<Sprite> something = new ArrayList<Sprite>();, and then something.add(new Sprite(spriteSheet, numRows, numColumns, true, x, y));.
EDIT:
Iteration of an ArrayList works like this:
List<Sprite> sprites = new ArrayList<Sprite>();
for(Sprite sprite : sprites)
{
sprite.print();
}
However, the following statement:
List<Sprite> sprites = new ArrayList<Sprite>();
for(Sprite sprite : sprites)
{
sprites.remove(sprite);
}
Will throw a ConcurrentModificationException.
Therefore, you can either use an iterator manually:
List<Sprite> sprites = new ArrayList<Sprite>();
Iterator<Sprite> iterator = sprites.iterator();
while(iterator.hasNext())
{
Sprite sprite = iterator.next();
...
iterator.remove();
}
Or, personally when I need to modify the elements on the list during iteration, I use the following:
List<Sprite> sprites = new ArrayList<Sprite>();
for(int i = 0; i < sprites.size(); i++)
{
Sprite sprite = sprites.get(i);
...
sprites.remove(i--);
}
Although if I remember correctly, the iterator is the recommended approach.

animate specific sprite with multiple sprite on android using andengine

i have multiple objects on my canvas. and after some condition, i want some of my sprite do animate. here my code:
private AnimatedSprite[] sign;
sign = new AnimatedSprite[9];
// some loop code to create 9 sign
..
sign[index] = new AnimatedSprite(x, y, myregion);
..
until this part is ok, all signs is on position. but when i want to animate some sprite, all of that sprite will do animate too. here the code:
while(signIndex<9)
{
if(signIndex==winSlot[0] || signIndex==winSlot[1] || signIndex==winSlot[2])
{
grupSign= null;
grupSign= sign[signIndex];
grupSign.animate(200, true);
}
signIndex++;
}
anyone know and can help me how to make only specific sprites do animate?
As per my suggestion you have to use deepCopy() method while you create your animated sprite object. As per the following
sign[index] = new AnimatedSprite(x, y, myregion.deepCopy());
Advantage of using deepCopy() method is that each time new region will created for your sprite.

Add dynamic path to sprite

I have to move the sprite along the path that is drawn onTouch.For that I'm using Path and PathModifier
case MotionEvent.ACTION_UP:
int historySize = pSceneTouchEvent.getMotionEvent().getHistorySize();
pointX = new float[historySize];
pointY = new float[historySize];
for (int i = 1; i < historySize; i++) {
pointX[i] = pSceneTouchEvent.getMotionEvent().getHistoricalX(i);
pointY[i] = pSceneTouchEvent.getMotionEvent().getHistoricalY(i);
}
path = new path(pointX,pointY);
PathModifier pathModifier = new PathModifier(2.5f, path);
pathModifier.setRemoveWhenFinished(true);
sprite1.clearEntityModifiers();
sprite1.registerEntityModifier(pathModifier);
break;
Its giving me error as path needs at least 2 way points.
Any idea why so?
Normally this shouldn't happen, since a motion event is very often more than just one coordinate. Maybe you should test if the historySize is really bigger than 2. In addition you can add the sprites starting coordinates, otherwise the sprite would "jump" towards the first touch point (but that wasn't your question).
This isn't actually different – just another possibility:
path= new Path(historySize);
for (int i = 0; i < historySize; i++) {
float x = pSceneTouchEvent.getMotionEvent().getHistoricalX(i);
float y = pSceneTouchEvent.getMotionEvent().getHistoricalY(i);
path.to(x,y);
}
In addition I noticed you start your for-loop with int i=1 so if your historySizeis 2, the loop iterates only one times!
EDIT
I couldn't find the problem, but I found a solution:
Instead of using the motionEvent history, save the coordinates of the toucheEvent on the go as the touchEventoccurs:
ArrayList<Float> xCoordinates; // this is where you store all x coordinates of the touchEvents
ArrayList<Float> yCoordinates; // and here will be the y coordinates.
onSceneTouchEvent(TouchEvent sceneTouchEvent){
switch(sceneTouchEvent.getAction()){
case (TouchEvent.ACTION_DOWN):{
// init the list every time a new touchDown is registered
xCoordinates = new ArrayList<Float>();
yCoordinates = new ArrayList<Float>();
break;
}
case (TouchEvent.ACTION_MOVE): {
// while moving, store all touch points in the lists
xCoordinates.add(sceneTouchEvent.getX());
yCoordinates.add(sceneTouchEvent.getY());
break;
}
case (TouchEvent.ACTION_UP): {
// when the event is finished, create the path and make the sprite move
// instead of the history size use the size of your own lists
Path path = new Path(xCoordinates.size());
for (int i = 0; i < xCoordinates.size(); i++) {
path.to(xCoordinates.get(i), yCoordinates.get(i)); // add the coordinates to the path one by one
}
// do the rest and make the sprite move
PathModifier pathModifier = new PathModifier(2.5f, path);
pathModifier.setAutoUnregisterWhenFinished(true);
sprite1.clearEntityModifiers();
sprite1.registerEntityModifier(pathModifier);
break;
}
}
I tested this on my phone (which does not run in debug mode) and it works fine. But to make sure that no Exception will be thrown, you should always test if the xCoordinates list is bigger than 1. Although it is very probable that it is.
Well I hope it helps at least to go around your original problem. I noticed that some methods are named differently (e.g. setAutoUnregisterWhenFinished(true);) I guess you are using AndEngine GLES1 ? I use GLES2, so when a method has another name in my code, don't worry and just look for the equivalent in GLES1 (I didn't rename them because, the code works as it is)
Christoph

Does AChartEngine support real-time values adding and x-scrolling

In order to display real-time digital input/output value like oscilloscope
There is no method to do x-scrolling, but you can create your own to do this. This is an example.
for (int i = values.getItemCount() - 2; i >= 0; i--) {
values.add(i + 1, value.getY(i));
}
values.add(0, y);
"values" is a TimeSeries.
Basically I swap penultimate value to the last value, and so on. I add, finally, the new value in position 0.
mSeries.add(++xCounter, aData);
if (mChartView != null) {
if (mSeries.getItemCount() >= 200) {
mSeries.remove(0);
}
mhartView.repaint();
}
can help here to draw dynamic view. 200 is your view buffer size, so once it get filled it start to remove from top and add new value at tail.

Integer Array Handling

I have a 2D game that uses two integer arrays to track x and y coordinates as shown below:
private int gridX[] = { 0,0,0,0,0 }
private int gridY[] = { 0,0,0,0,0 }
The problem is I can have a LOT of objects on the screen that needs to be tracked. Is there a way to add integers / create new blocks as needed? IE in a loop, do something like
gridX[].add(); or something like that. I'm relatively new to java and droid development and I'm having trouble finding a good tutorial or example that shows how to do this without having to initialize the gridX and gridY to sizes of 100 or so.
This is important, as I am about 90% sure that all those unused 0's are causing androids garbage cleanup to lag my application.
Why dont you use an array list instead of an Integer array?
that way you can dynamically add items to the list
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1);
myList.add(2);
Why not also use the Point class?
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0));
points.add(new Point(50, 70));
Point point = points.get(1);
Log.d("MyApp", String.format("The point is: (%d, %d)", point.x, point.y);
This way you are keeping track of your x and y coordinates together and there is less opportunity for error.

Categories

Resources