android code not working for sprite sheet - android

Can any one sort the problem in the code..i.e "The constructor CCSprite(String) is not visible".All the required imports have been imported, i have commented the error on the specified line where it is anoying me. thanks in advance
public class Sprite extends CCLayer{
CCSprite mSprite;
protected Sprite() {
super();
CGSize winSize = CCDirector.sharedDirector().winSize();
mSprite =new CCSprite("sprite.png");//Error:The constructor CCSprite(String) is not visible
mSprite.setPosition(CGPoint.ccp(mSprite.getContentSize().width/2.0f, mSprite.getContentSize().height/2.0f));
addChild(mSprite);
}
}

The exception message is telling you that the constructor you call here is not visible, i.e. it is private or has package visibility. Are you sure the source you looked at is the source you are calling?

Related

Proper way of creating game objects classes in Andengine

I was just wondering, if you want to create an object for a game, lets say a ball,
Should you create a class called ball that it contains the sprite and the body?, like:
public class Ball {
BaseGameActivity mActivity;
Sprite s;
Body mBody
public Ball(TextureRegion texture,BaseGameActivity mActivity){
this.mActivity=mActivity;
s= new Sprite (0,0,texture,mActivity.getVertexBufferObjectManager());
mBody=PhysicsFactory.createBoxBody(mActivity.mWorld,s, BodyDef.BodyType.StaticBody,PhysicsFactory.createFixtureDef(1,0,2));
//then it shoud be properly attached to scene and world etc...
}
}
A class that extends sprite? like:
public class Ball extends Sprite {
BaseGameActivity mActivity;
Body mBody
public Ball(TextureRegion texture,BaseGameActivity mActivity){
super (0,0,texture,mActivity.getVertexBufferObjectManager());
this.mActivity=mActivity;
mBody=PhysicsFactory.createBoxBody(mActivity.mWorld,this, BodyDef.BodyType.StaticBody,PhysicsFactory.createFixtureDef(1,0,2));
//then it shoud be properly attached to scene and world etc...
}
}
Or another way?
Which is the fastest? Which is the easier? Or the way doesn't matter?
Thanks in advance
just do like this.. u will probably need other sprites and other stuff...
public class Ball extends BaseGameActivity{
//constructor and other methods
BitmapTextureAtlas ballAtlas;
ITextureRegion ballTexture;
//in onLoadResources load ur atlas
//create texture region
//then in onCreateScene create a scene
//simply attach ur sprite ball to ur scene
}
you can create sprites anywhere.. you are not bound to create separate class for that
I think you can use both approach, it depends on you and your coding style / application infrastructure. :)
Altough I use a generic SceneManager class with a reference to the activity, instead of multiple activities

Error while trying to execute Sugar database CRUD operation

I (having mediocre developing skills) actually try to use Sugar as a database wrapper for my android project.
Therefore, I was following along the "Getting-Started-Guide" (http://satyan.github.io/sugar/getting-started.html) to get ready as soon as possible.
I created a class for my entities, called DataSet.java :
import com.orm.SugarRecord;
public class DataSet extends SugarRecord{
int someData;
double evenMoreData;
public DataSet(Context ctx){
super(ctx);
}
public DataSet(Context ctx,
int someData,
long evenMoreData) {
super(ctx);
this.someData = someData;
this.evenMoreData = evenMoreData;
}
}
I call the class in the following way:
someGreatClass something;
someMoreGreatCode somemore;
DataSet dataSet = new DataSet(
ctx, // Here Eclipse throws the error
something.method(),
somemore.anothermethod());
DataSet.save();
When I try to build this and to push it onto my device, Eclipse refuses to compile and throws this error:
ctx cannot be resolved to a variable
Considering the fact that I'm relatively new to Android development, the error may be obvious and I hope to get a tip how to solve this.
P.S.: Furthermore, I don't fully get the developer's statement in the getting-started-Note:
Please retain one constructor with Context argument. (This constraint will be removed in subsequent release.)
Thank you very much!
// Edit: Did edit the class name from LocationDataSet to Data set for clarification
First of all, the getting-started-note tells you that you need a constructor with only a context parameter, you did this here so that's ok
public DataSet(Context ctx){
super(ctx);
}
about
ctx cannot be resolved to a variable
I think you don't have a variable called ctx, I don't know if you're familiar with android context? (basically a context is a service or an activity), if you're using this code in an activity or a service, just use the 'this' keyword and not the ctx variable
The code you provide doesn't really show what you're doing, but you showed us the code from 'DataSet', but the error happens with a LocationDataSet? And you're calling save on DataSet?
The save method must be called on an object, not a class.
Also don't forget that sugar needs the special application class in the manifest
UPDATE with example:
Your dataset class (the sugarrecord) should look like this, that's ok in your code as far as I can see
public class DataSet extends SugarRecord<DataSet>{
private String someData;
public DataSet(Context c){
super(c);
}
public DataSet(Context c, String someData){
super(c);
this.someData = someData;
}
}
An activity that uses the record should look like this
public class SomeActivity extends Activity {
public void someMethodThatUsesDataSet(){
// Create a dataset object with some data you want the save and a context
// The context we use here is 'this', this is the current instance of SomeActivity,
// you absolutely need this, I think this is what you're doing wrong,
// you can't use ctx here because that's not a known variable at this point
DataSet example = new DataSet(this, "data you want to save");
// Tell Sugar to save this record in the database
example.save();
}
}

passing instructions from UI thread to Rendering thread (GLSurfaceView)

The Setup : A RelativeLayout with a GLSurfaceView and a Button as shown in the image..
The Problem: Lets say I have other triangle models (The one in the picture being the initial model)... I wish to change the models cyclically on click of the button. Since button is on the UI thread and glSurfaceView runs on a separate thread, I don't exactly know how to pass the info/instruction to it. I know there is this thing called Handler in Android which might be useful in this case... But I need some help here..
Edit: If Handler is the right way, I need to know how to add Looper to that Handler... The documentation says add looper.prepare() at the start of run() method.. But the glSurfaceView creates thread implicitly, resulting in no run() method directly available..
I don't think it is necessary to use handlers to solve this issue but you may need to adjust the way you organise your classes.
Here is an example of an organisational structure that might solve your issue:
Activity Class
public class MainActivity extends Activity {
private int modelNumber = 0;
private ArrayList<Model> models = new ArrayList<Model>();
private YourRendererClass renderer;
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Setup GLSurfaceView
GLSurfaceView surface = new GLSurfaceView(this);
setContentView(surface);
renderer = new YourRendererClass();
surface.setRenderer(renderer);
// Set up models
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
etc.
// Display first model
renderer.setCurrentModel(models.get(modelNumber));
...
}
// Called by the button press:
// Use android:onClick="onClick"
// in your layout xml file within button
public void onClick(View view){
// Make it loop round
modelNumber++;
if(modelNumber>=models.size()){
modelNumber=0;
}
// Display current model
renderer.setCurrentModel(models.get(modelNumber));
}
}
Renderer Class
public class YourRendererClass implements Renderer {
private Model currentModel;
#Override
public void onDrawFrame(GL10 gl) {
// ** Your existing set-up code **//
// Draw model
if (currentModel!=null){
currentModel.draw(gl);
}
}
public void setCurrentModel(Model model){
currentModel = model;
}
}
Model class
public class Model {
// Holds model information
private int size;
private int x;
private int y;
// etc...
public model(int x, int y, int size etc...){
this.x=x;
this.y=y;
this.size=size;
// etc....
}
public void draw(GL10 gl) {
// ** Draw model based on model information fields above **
}
}
The above code is untested as I don't have access to your drawing code but the structure should work if implemented correctly. I've tried to make it clear where you'll have to insert your own code to make it work. In particular I wasn't sure what defines each of your different models so you'll need to include sufficient local variables within the Model class to define them.
I hope my answer helps, let me know if you have any questions.
Tim
You should look at queueEvent! It's a very convenient way to pass informations from UI Thread to renderer Thread:
queueEvent(new Runnable(){
#Override
public void run() {
mRenderer.method();
}});

Cocos2D Android How to have a scene object?

I can get the scene from a layer, but I don't know how to have a Scene that manages layers. I can do this on iPhone, but on Android my code doesn't even get called.
This is in my start Activity:
CCDirector.sharedDirector().runWithScene(EditorScene.node());
This is my scene class:
public class EditorScene extends CCScene
{
public EditorScene()
{
CanvasBackgroundLayer canvasBackgroundLayer = (CanvasBackgroundLayer) CanvasBackgroundLayer.node();
CanvasEditorLayer canvasEditorLayer = (CanvasEditorLayer) CanvasEditorLayer.node();
addChild(canvasBackgroundLayer,0);
this.addChild(canvasEditorLayer);
}
}
My constructor here is never called. Any ideas on what to do?
Any subclasses of CCScene have to reimplement the static method node(). Have a look at the bottom of CCScene.h.

Android : NullPointerException while adding Resources

I'm getting a NullPointerException when running this bit of code
abstract class thing extends Drawable(){
Bitmap sprite;
int spriteResource;
public thing(){
setResources();
sprite=Bitmap.createBitmap(sprite,src.left,src.top,(src.right-src.left),(src.bottom-src.top),m,true);
}
#Override
public void draw(Canvas c){
bit= Bitmap.createBitmap(sprite,0,0,45, 45);// Generates the exception
c.drawBitmap(bit, x, y, null);
}
abstract void setResource();
}
class otherThing extends thing(){
#Override
public void setResource(){
spriteResource=R.drawable.otherThing_sprite;
}
}
Basically i'm trying to load different sprites into different classes by using the method of the parent class. But the spriteResource doesn't get set and I can't understand why?
I set up the log which returned the Resource as 0. Any ideas why this is happening or how to resolve it???
Thanks
When createBitmap in thing() is trying to give the sprite variable a value, sprite is one of its in-parameters, this means that you are trying to use sprite before it is created and you get a null pointer exception.

Categories

Resources