Here is the code in the class,I am using libgdx to develop a game but i don't how to solve the error about
(Cannot resolve constructor 'Sprite(java.lang.Object)' )in the render.
public class GamePage implements Screen{
private static final float FRAME_DURATION = 1.0f / 15.0f;
private SpriteBatch batch;
private TextureAtlas boxAtlas;
private Animation boxAnim;
private float boxTime;
private Sprite spritebox;
#Override
public void show() {
boxAtlas = new TextureAtlas(Gdx.files.internal("ybanim.pack"));
boxAnim = new Animation(FRAME_DURATION, boxAtlas.getRegions(), Animation.PlayMode.NORMAL);
batch = new SpriteBatch();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
boxTime += delta;
spritebox = new Sprite(boxanim.getKeyFrame(boxTime, true));
spritebox.setPosition(0,0);
batch.begin();
spritebox.draw(batch);
batch.end();
}
boxanim.getKeyFrame(boxTime, true) returning Object and no constructor available in Sprite class that can take Object as parameter.
From gdx version 1.9.5 Animation class is now generic, there is a pull request for the same. so declare your animation in this way :
Animation<TextureRegion> boxAnim;
Related
I want to resize a UI panel with the mouse. I don't know how to do this myself. How can I do this?
Kind of a short question, so I'm not sure if you want anything extremely specific, but this is something I found on the Unity Tutorials Website. Seems to be what you're looking for:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
public Vector2 minSize;
public Vector2 maxSize;
private RectTransform rectTransform;
private Vector2 currentPointerPosition;
private Vector2 previousPointerPosition;
void Awake () {
rectTransform = transform.parent.GetComponent<RectTransform>();
}
public void OnPointerDown (PointerEventData data) {
rectTransform.SetAsLastSibling();
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);
}
public void OnDrag (PointerEventData data) {
if (rectTransform == null)
return;
Vector2 sizeDelta = rectTransform.sizeDelta;
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition);
Vector2 resizeValue = currentPointerPosition - previousPointerPosition;
sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y);
sizeDelta = new Vector2 (
Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
);
rectTransform.sizeDelta = sizeDelta;
previousPointerPosition = currentPointerPosition;
}
}
I have created a CollapsablePane which shows an EffectImageView as collapsableContent, and a Node as mainContent. The EffectImageView consists of two layered ImageViews, the bottom one having an BoxBlur effect applied on it.
When there is a ScrollEvent on the mainContent the collapsableContent will be animated accordingly by setTranslateY.
With a BoxBlur effect applied, the translation of the lower part of the collapsableContent is very unsmooth, while it is absolutely smooth without the effect.
Is there a possibility to improve the transition performance?
public class EffectImageView extends Pane {
private ImageView img;
private ImageView imgEffect;
private Rectangle clipTop;
private Rectangle clipBottom;
private DoubleProperty imageEffectHeight = new SimpleDoubleProperty();
private double effectOffset;
public EffectImageView() {
img = createCachedImageView();
imgEffect = createCachedImageView();
imgEffect.imageProperty().bind(img.imageProperty());
imgEffect.fitWidthProperty().bind(img.fitWidthProperty());
imgEffect.fitHeightProperty().bind(img.fitHeightProperty());
clipTop = new Rectangle();
clipTop.widthProperty().bind(img.fitWidthProperty());
clipTop.heightProperty().bind(img.fitHeightProperty().subtract(imageEffectHeight).subtract(translateYProperty()));
clipBottom = new Rectangle();
clipBottom.widthProperty().bind(img.fitWidthProperty());
clipBottom.heightProperty().bind(imageEffectHeight);
clipBottom.translateYProperty().bind(heightProperty().subtract(imageEffectHeight).subtract(translateYProperty()));
img.setClip(clipTop);
imgEffect.setClip(clipBottom);
getChildren().addAll(img, imgEffect);
}
private ImageView createCachedImageView() {
ImageView img = new ImageView();
img.setCache(true);
img.setCacheHint(CacheHint.SPEED);
return img;
}
public final ObjectProperty<Image> imageProperty() {
return img.imageProperty();
}
public final Image getImage() {
return img.getImage();
}
public final void setImage(Image image) {
img.setImage(image);
}
public void setImageEffect(Effect effect) {
imgEffect.setEffect(effect);
}
public final DoubleProperty imageEffectHeightProperty() {
return imageEffectHeight;
}
public void setEffectOffset(double offset) {
effectOffset = offset;
}
#Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
img.relocate(-effectOffset, 0);
imgEffect.relocate(-effectOffset, 0);
img.setFitWidth(w + effectOffset * 2);
img.setFitHeight(h);
}
}
I'm trying to create animation but keep getting errors
package com.leopikinc.bobdestroyer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class MainMenu implements Screen{
Texture background_main;
TextureRegion[] background_textures;
Animation background_animation;
SpriteBatch batch;
TextureRegion current_frame;
float stateTime;
BobDestroyer game;
public MainMenu(BobDestroyer game){
this.game = game;
}
#Override
public void show() {
background_main = new Texture(Gdx.files.internal("main_menu_screen/Background.png"));
//TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72);
//background_textures = new TextureRegion[3];
for(int i = 0; i<3; i++){
background_textures[i] = new TextureRegion(background_main,0, 0+72*i, 128, 72+72*i);
}
background_animation = new Animation(0.2f,background_textures);
}
#Override
public void render(float delta) {
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
current_frame = background_animation.getKeyFrame(stateTime, true);
batch.begin();
batch.draw(current_frame, 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
this is my code and then i try to launch it i get
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.leopikinc.bobdestroyer.MainMenu.show(MainMenu.java:31)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.leopikinc.bobdestroyer.BobDestroyer.create(BobDestroyer.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
How to fix this? And also when i use TextureRegion[][] temp = new TextureRegion.split(background_main, 128, 72);Eclipse says
TextureRegion.split cannot be resolved to a type
TextureRegion.split cannot be resolved to a type
You are getting an error because you are using keyword new
Do something like this and your error will be fixed
TextureRegion[][] tmp = TextureRegion.split(background_main, background_main.getWidth()/TOTAL_COLS, background_main.getHeight()/TOTAL_ROWS);
Your second error is because you have commented the line where you are initializing your array, you need to uncomment this
//background_textures = new TextureRegion[3];
I am having a difficult time implementing the Universal Tween Engine in my android, desktop, and html libgdx projects (Note that this is a problem with libgdx version 1.3.1 that uses gradle).
I have followed the instructions on the github wiki by using the fileTree method shown in the link below.
https://github.com/libgdx/libgdx/wiki/Universal-Tween-Engine
I managed to set the build path of the CORE and ANDROID projects to include the two tween engine jars. The classes get imported easily but when runnning the DESKTOP project I get the following error:
Exception in thread "LWJGL Application" java.lang.RuntimeException: No TweenAccessor was found for the target
at aurelienribon.tweenengine.Tween.build(Tween.java:787)
at aurelienribon.tweenengine.Tween.build(Tween.java:79)
at aurelienribon.tweenengine.BaseTween.start(BaseTween.java:85)
at aurelienribon.tweenengine.TweenManager.add(TweenManager.java:61)
at aurelienribon.tweenengine.BaseTween.start(BaseTween.java:98)
at com.infinitybit.killtheclouds.Screens.Splash.show(Splash.java:58)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.infinitybit.killtheclouds.KillTheClouds.create(KillTheClouds.java:14)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Here is my code
Splash.java
package com.infinitybit.killtheclouds.Screens;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.infinitybit.killtheclouds.KillTheClouds;
import com.infinitybit.killtheclouds.TweenStuff.SplashAccessor;
public class Splash implements Screen {
public static int SCREEN_WIDTH = Gdx.graphics.getWidth();
public static int SCREEN_HEIGHT = Gdx.graphics.getHeight();
private KillTheClouds game;
private SpriteBatch splashpaper;
private Sprite splashimage;
private TweenManager tweenManager;
public Splash(KillTheClouds game) {
this.game = game;
}
#Override
public void show() {
// TweenSTUFF
tweenManager = new TweenManager();
Tween.registerAccessor(Sprite.class, new SplashAccessor());
splashpaper = new SpriteBatch();
Texture splashTexture = new Texture("images/splash/logo.png");
splashimage = new Sprite(splashTexture);
// image size
splashimage.setSize(SCREEN_WIDTH / 1.5f, SCREEN_HEIGHT / 2);
// center image
splashimage.setPosition(SCREEN_WIDTH / 2 - splashimage.getWidth() / 2,
SCREEN_HEIGHT / 2 - splashimage.getHeight() / 2);
Tween.set(splashTexture, SplashAccessor.ALPHA).target(0)
.start(tweenManager);
Tween.to(splashTexture, SplashAccessor.ALPHA, 3).target(1)
.start(tweenManager);
Tween.to(splashTexture, SplashAccessor.ALPHA, 2).target(0).delay(2)
.start(tweenManager);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// update tween
tweenManager.update(delta);
splashpaper.begin();
splashimage.draw(splashpaper);
splashpaper.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
SplashAccessor.java
package com.infinitybit.killtheclouds.TweenStuff;
import aurelienribon.tweenengine.TweenAccessor;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class SplashAccessor implements TweenAccessor<Sprite> {
public static final int ALPHA = 0;
#Override
public int getValues(Sprite target, int tweenType, float[] returnValues) {
switch (tweenType) {
case ALPHA:
returnValues[0] = target.getColor().a;
return 1;
default:
assert false;
return -1;
}
}
#Override
public void setValues(Sprite target, int tweenType, float[] newValues) {
float defRed = target.getColor().r;
float defGreen = target.getColor().g;
float defBlue = target.getColor().b;
switch(tweenType){
case ALPHA:
target.setColor(defRed, defGreen, defBlue, newValues[0]);
break;
default:
assert false;
}
}
}
Your splashTexture isn't the type of Sprite it is Texture!!!
That is your problem.
Create splashSprite from splashTexture, and call this:
Tween.to(splashSprite, SplashAccessor.ALPHA, 3).target(1)
.start(tweenManager);
Hope it helps!
i'm using universal image loader to populate thumbnails into gridview from assets gallary but images are loading slowly during scrolling up or down so i think because images are being loaded with it's current resolution so how i can change the resolution of the images for example i used to use createScaledBitmap to scale down the bitmap
Here is my code :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<String> mList;
private int mheight;
private int mwidth;
private InputStream is;
private HomePage homePage;
private ImageLoader imageLoader;
public ImageAdapter(Context context, List<String> list, int height, int width) {
mContext = context;
mList = list;
mheight = height;
mwidth = width;
ImageLoader imageLoader = ImageLoader.getInstance();
this.imageLoader = imageLoader;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position).toString();
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
File cacheDir = new File(Environment.getExternalStorageDirectory(), "UniversalImageLoader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
.threadPoolSize(5)
.memoryCacheExtraOptions(mwidth/3, mwidth/3)
.threadPriority(Thread.MIN_PRIORITY )
.memoryCache(new UsingFreqLimitedMemoryCache(5000000)) // You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.build();
imageLoader.init(config);
//
//
//display options
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.cacheInMemory()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.build();
// // Create configuration for ImageLoader
String imString = mList.get(position);
String imageUria = "assets://"+imString;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(mwidth/3, mwidth/3));
imageLoader.displayImage(imageUria, imageView ,options );
return imageView ;
}
}