Skip to content

scrollcam component#898

Open
itzKiwiSky wants to merge 31 commits intokaplayjs:masterfrom
itzKiwiSky:master
Open

scrollcam component#898
itzKiwiSky wants to merge 31 commits intokaplayjs:masterfrom
itzKiwiSky:master

Conversation

@itzKiwiSky
Copy link
Copy Markdown
Contributor

added a new component, the scrollcam component implements a simple parallax system inspired by haxeflixel functions like setScrollFactor.

the example test also available for further test and verifications.

Summary

  • scrollcam component
  • test for the component
2025-09-21.21-39-00.mp4

* @param x number
* @param y number
*/
setScrollFactor(x: number, y: number): void;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be ...args: Vec2Args rather than hardcoding x and y

* @subgroup Component Types
*/
export interface CamScrollComp extends Comp
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run pnpm fmt

* @returns The current factor of the object as a {@link Vec2 `Vec2`}
*/
getScrollFactor(): Vec2;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is basePos not exposed on the interface

//@ts-ignore
basePos: null as Vec2 | null,

add()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this add hook does nothing so remove it

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Sep 22, 2025

Open in StackBlitz

npm i https://pkg.pr.new/kaplayjs/kaplay@898

commit: 60f1bca

update()
{
const obj = this as unknown as GameObj<PosComp> & { basePos: Vec2 | null };
update() {
Copy link
Copy Markdown
Contributor

@dragoncoder047 dragoncoder047 Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just use update(this: GameObj<PosComp | CamScrollComp>) {

},

setScrollFactor(x: number, y: number) {
setScrollFactor(x: number, y: number) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also vec2args here

},

getScrollFactor() { return this.factor.clone() },
getScrollFactor() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also are thes necessary when you can access the factor property (it's not private / in the closure)

Comment thread src/core/contextType.ts Outdated
* @param y number
* @returns the camera scroll comp
*/
scrollcam(x: number, y: number): CamScrollComp;

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is bug??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now should be fixed

},

getScroll() {
return this.factor.clone();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have getScroll if factor is public? Was factor supposed to be a closure variable instead maybe?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably meant

    let factor = vec2(...args);
    let basePos = vec2();
    return {
        id: "scroll",
        require: ["pos"],

So they are inaccessible from outside

},

getScroll() {
return this.factor.clone();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably meant

    let factor = vec2(...args);
    let basePos = vec2();
    return {
        id: "scroll",
        require: ["pos"],

So they are inaccessible from outside

@lajbel lajbel added this to the 4000.0.0-alpha.24 milestone Nov 4, 2025
Comment thread src/core/contextType.ts
fixed(fixed?: boolean): FixedComp;

/**
* Make the object scrolls with camera based on a factor
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a mention of "parallax scrolling" since that's what this component also does.

it might be more intuitive to rename the component as well to parallax() but that's up to you

update(this: GameObj<PosComp | CamScrollComp>) {
const cam = getCamPos();

if (!this.basePos) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line will never work because you're setting it intially to the zero vector on line 29 which is an object and considered truthy

@lajbel lajbel removed this from the 4000.0.0-alpha.24 milestone Jan 1, 2026
Comment thread src/ecs/components/transform/camscroll.ts Outdated
@itzKiwiSky
Copy link
Copy Markdown
Contributor Author

I made some changes to the parallax component because the old code has been broken, the effect has not being applied so, I used the literal of MoveBy to fix it


const delta: Vec2 = cam.sub(this.lastCam);
this.moveBy(
this.pos = this.pos.add(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moveBy does exactly this. You're saying it isn't working?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was about organization, I will return back to the original version instead

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might remove the pos setter/getter in the future and have only moveTo, moveBy and getPos. So it's better to leave moveBy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea I notice I "can't" set the pos directly, that's why broke the old version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea I notice I "can't" set the pos directly, that's why broke the old version.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can, just not pos.x or pos.y separately, you need to replace the entire pos. That's why we're thinking of removing that property.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea make sense, will be for good if those properties be private, only internal access

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants