Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tried to access an image "characters/amiy/idle.png" that does not exist in the cache. Make sure to load() an image before accessing it #3144

Closed
huanmoon opened this issue May 3, 2024 · 3 comments
Labels

Comments

@huanmoon
Copy link

huanmoon commented May 3, 2024

What happened?

Maybe it's not a bug, but it's pretty weird, so my Player class requires a picture, and I'm sure I've loaded that before creating the player class but that's not working at all and warns
`he following assertion was thrown building FutureBuilder(dirty, state:
_FutureBuilderState#19a16):
Tried to access an image "characters/amiy/idle.png" that does not exist in the cache. Make
sure to
load() an image before accessing it
'package:flame/src/cache/images.dart':
Failed assertion: line 104 pos 7: 'asset != null'

The relevant error-causing widget was:
FutureBuilder
FutureBuilder:file:///home/huan_moon/.pub-cache/hosted/pub.flutter-io.cn/flame-1.17.0/lib/s
rc/game/game_widget/game_widget.dart:389:30

When the exception was thrown, this was the stack:
#2 Images.fromCache (package:flame/src/cache/images.dart:104:7)
#3 Player.onLoad (package:amiys_island/entities/player.dart:36:28)
#4 Component._startLoading (package:flame/src/components/core/component.dart:856:26)
#5 Component._addChild (package:flame/src/components/core/component.dart:606:20)
#6 Component.add (package:flame/src/components/core/component.dart:568:46)
#7 AmiysIsland.onLoad (package:amiys_island/amiys_island.dart:25:9)
#8 Game._onLoadFuture (package:flame/src/game/game.dart:71:45)
#9 Game._onLoadFuture (package:flame/src/game/game.dart)
#10 Game.load (package:flame/src/game/game.dart:83:12)
#11 FlameGame.load (package:flame/src/game/flame_game.dart:105:17)
#12 GameWidgetState.loaderFuture.
(package:flame/src/game/game_widget/game_widget.dart:194:20)
#13 GameWidgetState.loaderFuture
(package:flame/src/game/game_widget/game_widget.dart:199:9)
#14 GameWidgetState.build...
(package:flame/src/game/game_widget/game_widget.dart:390:33)
#15 GameWidgetState._protectedBuild
(package:flame/src/game/game_widget/game_widget.dart:228:21)
#16 GameWidgetState.build..
(package:flame/src/game/game_widget/game_widget.dart:376:28)
#17 _LayoutBuilderElement._layout.layoutCallback
(package:flutter/src/widgets/layout_builder.dart:139:77)
#18 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2844:19)
#19 _LayoutBuilderElement._layout
(package:flutter/src/widgets/layout_builder.dart:173:12)
#20 RenderObject.invokeLayoutCallback.
(package:flutter/src/rendering/object.dart:2686:59)
#21 PipelineOwner._enableMutationsToDirtySubtrees
(package:flutter/src/rendering/object.dart:1097:15)
#22 RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:2686:14)
#23 RenderConstrainedLayoutBuilder.rebuildIfNecessary
(package:flutter/src/widgets/layout_builder.dart:248:7)
#24 _RenderLayoutBuilder.performLayout
(package:flutter/src/widgets/layout_builder.dart:331:5)
#25 RenderObject.layout (package:flutter/src/rendering/object.dart:2575:7)
#26 RenderBox.layout (package:flutter/src/rendering/box.dart:2389:11)
#27 RenderProxyBoxMixin.performLayout
(package:flutter/src/rendering/proxy_box.dart:105:21)
#28 RenderObject.layout (package:flutter/src/rendering/object.dart:2575:7)
#29 RenderBox.layout (package:flutter/src/rendering/box.dart:2389:11)
#30 RenderProxyBoxMixin.performLayout
(package:flutter/src/rendering/proxy_box.dart:105:21)
#31 RenderObject.layout (package:flutter/src/rendering/object.dart:2575:7)
#32 RenderBox.layout (package:flutter/src/rendering/box.dart:2389:11)
#33 RenderProxyBoxMixin.performLayout
(package:flutter/src/rendering/proxy_box.dart:105:21)
#34 RenderObject.layout (package:flutter/src/rendering/object.dart:2575:7)
#35 RenderBox.layout (package:flutter/src/rendering/box.dart:2389:11)
#36 RenderProxyBoxMixin.performLayout
(package:flutter/src/rendering/proxy_box.dart:105:21)
#37 RenderObject.layout (package:flutter/src/rendering/object.dart:2575:7)
#38 RenderBox.layout (package:flutter/src/rendering/box.dart:2389:11)
#39 RenderView.performLayout (package:flutter/src/rendering/view.dart:195:14)
#40 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:2414:7)
#41 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1051:18)
#42 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1064:15)
#43 RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:582:23)
#44 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:991:13)
#45 RendererBinding._handlePersistentFrameCallback
(package:flutter/src/rendering/binding.dart:448:5)
#46 SchedulerBinding._invokeFrameCallback
(package:flutter/src/scheduler/binding.dart:1386:15)
#47 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#48 SchedulerBinding.scheduleWarmUpFrame.
(package:flutter/src/scheduler/binding.dart:1034:7)
#52 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 5 frames from class _AssertionError, class _Timer, and dart:async-patch)`

and this is my code

    @override
    Color backgroundColor() {
        return const Color(0xff5fcde4);
    }

    @override
    FutureOr<void> onLoad() async {
        print('Amiy\'s Island is loading...');
        
        images.loadAllImages();

        world = Island();
        camera = CameraComponent.withFixedResolution(width: 600, height: 600);
        camera.viewfinder.anchor = Anchor.topLeft;
        add(Player(camera));
        print('test2');

        return super.onLoad();
    }
}```

my player class

class Player extends SpriteAnimationGroupComponent with KeyboardHandler, HasGameRef {
CameraComponent camera;
double velocity = 1.0;
late final SpriteAnimation idleAnimation;

Player(this.camera);

@override
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
    if(keysPressed.contains(LogicalKeyboardKey.arrowLeft)) {
        camera.moveBy(Vector2(0-velocity, 0));
    }
    if(keysPressed.contains(LogicalKeyboardKey.arrowRight)) {
        camera.moveBy(Vector2(velocity, 0));
    }
    if(keysPressed.contains(LogicalKeyboardKey.arrowUp)) {
        camera.moveBy(Vector2(0, 0-velocity));
    }
    if(keysPressed.contains(LogicalKeyboardKey.arrowDown)) {
        camera.moveBy(Vector2(0, velocity));
    }

    return true;
}

@override
FutureOr<void> onLoad() {
    print('test1');
    idleAnimation = SpriteAnimation.fromFrameData(
        gameRef.images.fromCache('characters/amiy/idle.png'), 
        SpriteAnimationData.sequenced(
            amount: 4, 
            stepTime: 0.5, 
            textureSize: Vector2(32, 32)
        )
    );

    return super.onLoad();
}

}```

help is appreciated

@huanmoon huanmoon added the bug label May 3, 2024
@ufrshubham
Copy link
Member

ufrshubham commented May 3, 2024

@huanmoon have you added assets/images/characters/amiy/ to the assets section in your pubspec?

@ufrshubham
Copy link
Member

Also, you'll have to await for loadAllImages() to complete loading all the images before proceeding with rest of the game setup.

@huanmoon
Copy link
Author

huanmoon commented May 3, 2024

Also, you'll have to await for loadAllImages() to complete loading all the images before proceeding with rest of the game setup.

god I'm literally a stupid and I didn't add /images/ before my file name =(

@huanmoon huanmoon closed this as completed May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants